Capitalized - cattle-off

Title Description

For all the words in a string, if the first letter of the first letter of the word is not in capital letters, put words into uppercase letters. In the string, between words separated by white space, white space comprising: a space ( ''), a tab ( '\ T'), carriage return ( '\ R & lt'), line feed ( '\ n-' ).

Enter a description:

Input line: character string to be processed (length less than 100).

Output Description:

It may be multiple sets of test data for each data 
output line: the converted string.
Example 1

Entry

if so, you already have a google account. you can sign in on the right.

Export

If So, You Already Have A Google Account. You Can Sign In On The Right.

Analysis of problem-solving

In the string is scanned, and the number of characters using a recording interval preceding character recorded after the change is determined directly lowercase letters.

Note that the following points:

1, the input string

    Cin >> str using this method then the received character string entered str not contain spaces, the space which would otherwise follow the entire string is segmented into several segments.

  If the input string if you want to use it with spaces getline ()

#include <iostream>
#include <string>
using namespace std;

string s;
getline(cin,s);

2, the processing of the last word, since the end of the string using a '\ 0'

   Here I use the subscript mark the last character, to read the last word.

   You also need to do a separate deal on the last character.

3, C is the conversion of the letter case

  

        

  • Lowercase ASCII code large
  • Small capital letters ASCII code
  • Lowercase letters differ ASCII code 32
  • Digit ASCII code characters than the alphabet 0 48

Last code is as follows:

. 1 #include <the iostream>
 2 #include < String >
 . 3  the using  namespace STD;
 . 4  
. 5  int main ()
 . 6  {
 . 7      String S;
 . 8      getline (CIN, S);   // string input with the Spacer 
. 9      int COUNT = 0 ;
 10        int I = 0 ;
 . 11          for (I = 0 ; I <s.length (); I ++ )
 12 is          {
 13 is              IF (S [I] == '  ' || S [I] == '\t'||s[i]=='\r'||s[i] =='\n'||i==s.length()-1) 
14             {                                                  //考虑最后一个单词
15                 if(count!=0&&s[i-count]<='y'&&s[i-count]>='a') 
16                     s[i-count] = s[i-count]-32;
17                 count=0;
18             }
19             else
20                 ++ COUNT ;
 21 is          }
 22 is      IF (S [I- . 1 ] <= ' Y ' && S [I- . 1 ]> = ' A ' )      // if the last word is a single character 
23 is          IF (S [I- 2 ] = = '  ' )
 24              S [I- . 1 ] - = 32 ;
 25      COUT S << << endl;
 26 is }

 

 

Guess you like

Origin www.cnblogs.com/jiashun/p/newcode20.html