Some small trick C / C ++ in the spaces of the input string

Today we found some problems of their own existence at the time of review of C ++, hereby recorded.

We can look at the following code:

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
    string s;
  cin >> s;
    int cnt[26]={0};//字母统计次数
    for(int i = 0; i < s.length(); i++){
        char c = s[i];
        if(isalpha(c)){
            cnt[toupper(c) - 'A'] ++;
        }
    }
    for(int i = 0; i < 26; i++){
        if(cnt[i] != 0){
            cout << char(i + 'A') << ": " << cnt[i] <<endl;
        }
    }
    return 0;
}

When we enter a string Hello World!, the result would be?

apple@localhost  ~/Desktop/cpp_code  g++ run.cpp -o run.out -std=c++11 -O2
apple@localhost  ~/Desktop/cpp_code  ./run.out
Hello World!
E: 1
H: 1
L: 2
O: 1

The above code is not a good statistic for each letter string in which the frequency of occurrence, and I tried to think for a long time, only to find a problem to ignore a once off, cin standard input string in the face of spaces They will be cut off, and we need to enter a string with spaces for special treatment, and the use getline can be the perfect solution to the problem. getline (std :: cin, std :: string) will be cut off when entering a carriage return, enter the above code to make some adjustments can be the perfect solution to the problem.

ps: This method is for the string class

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;
int main(int argc, char const *argv[])
{
    string s;
    getline(cin, s);
    int cnt[26]={0};//字母统计次数
    for(int i = 0; i < s.length(); i++){
        char c = s[i];
        if(isalpha(c)){
            cnt[toupper(c) - 'A'] ++;
        }
    }
    for(int i = 0; i < 26; i++){
        if(cnt[i] != 0){
            cout << char(i + 'A') << ": " << cnt[i] <<endl;
        }
    }
    return 0;
}

When we enter the string again Hello World!, the result will be?

apple@localhost  ~/Desktop/cpp_code  g++ run.cpp -o run.out -std=c++11 -O2
apple@localhost  ~/Desktop/cpp_code  ./run.out
Hello World!
D: 1
E: 1
H: 1
L: 3
O: 2
R: 1
W: 1

This is very OK!

In addition, there is no other way to enter the string with spaces in it?

The answer is yes, I have all of the following situations may arise list them out.

String input sequences are known: a scene

For this case, we can directly input string at the time defined sequence can be, for example, the character sequence input we know we want to Hello World!, we can write the following definition:

str = "Hello World!";

Scenario 2: unknown sequence input string, but to know the maximum string length sequence

method one:

Getline function according to the above given, we can by calling the method:

cin.getline(str, len);

The first parameter str array name for storing input row, the second parameter is the number of characters to be read.

Method Two:

We can use an array of characters to solve this problem, this time we naturally think of the get method call as follows:

cin.get(str, len);

And a method of the same, both of which are read one line, until the line feed, and that the difference between the two line feeds getline discarded and the get () will be retained in the newline character in the input sequence, so we will consider the following rewritable the way:

while((c=cin.get())!='\n')

And if using C language library in the way, we should be how to express it?

Method three:

Enter a string in C language, our first thought is to use the scanf function, but scanf carriage returns and spaces are the default spacing between the input and the end symbol different groups, so the input string with a space, tab, or carriage return is not Yes, we can use format specifiers "% []." Its role is to scan a set of characters, called by:

scanf("%[^c]", str);

Wherein "c" is a constant specific character (including control characters). When the input character string, the character "c" is input as the current terminator. With this format can specify a character input by the programmer own terminator.

Method four:

Another C language input string in a way that gets function used. gets function as a terminator is a carriage return, call as follows:

char str[length]; 
gets(str);

Wherein the sequence length is the maximum length of a string is a specific value.

Scenario 3: unknown sequence input string, and the string of the maximum length sequence is also known

In response to this string class issue, this time we are only using getline function to solve the call as follows:

getline(cin, s);

Guess you like

Origin www.cnblogs.com/ECJTUACM-873284962/p/11619496.html