C + + Use Cin.getline Use example note point 2: Character count

Source: Internet
Author: User
Tags strlen

#include <iostream>#include<cstring>using namespacestd;voidBackaround (Char*s,Char*p);intMainintargcChar*argv[]) {    Chars[ -],p[ -]; inti; intJ; intLen; Cin.getline (s), -);//used to determine input data//cout<<s;Len = strlen (s);//This loop is used to separate sentence words .     for(i=0, j=0; i<len;i++){        if(s[i]!=' ') p[j++]=S[i]; Else{p[j]=' /'; Backaround (p,p+j-1);cout<<p<<" "; j=0;} } P[j]=' /'; Backaround (P,p+j-1); cout<<p; return 0;}voidBackaround (Char*s,Char*p) {    if(s>=p)return ; *s^=*p;//data transposition using XOR operation*p^=*s; *s^=*p; Backaround (S+1, P-1);}

The code has a reference: do not remember which is the reference, here is the introduction of Cin.getline () function use method, but also update, cin.getline (s,sizeof, ' a '); Enter a string, ending with a, but excluding a.

Put the following code:

1#include <iostream>2#include <cstring>3 using namespacestd;4 voidBackaround (Char*s,Char*p);5 intMainintargcChar*argv[])6 {7     Chars[ -];//p[500];8    //int i;9    //Int J;Ten     intLen; OneCin.getline (s), -,'.');//used to determine input data Acout<<s<<Endl; -Len =strlen (s); -cout << Len <<Endl; the}

You can see that the string output has no "." At the same time strlen calculation string does not put the end of the string to enter the count, the space calculation,

However, if the carriage return is used as a newline character, it will be counted as a char.

For now, Strlen calculates the return key used as a newline character, but does not calculate the return of the end of the string.

C + + Use Cin.getline Use example note point 2: Character count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.