2017 final English sentence format simple check (20 points)

L1-8 2017 final English sentence format simple check (20 points)

In English writing, the first letter of a sentence is usually uppercase, and the rest are lowercase, except for the word "I". Words are separated by a space. The sentence is broken with ",", and the end of the sentence is ended with ".", ", "And "." do not need a space to separate the preceding words. Text editors such as Word usually automatically correct the English sentences we enter according to the above rules. Please write a function to input an English sentence in the correct format according to the above rules. For example, for the input English sentence "This is an Example with one mistake.", since the character "E" in the word "Example" should be lowercase "e", the correct format of the sentence is output after revision This is an example with one mistake.

note:

1. Each set of test data only includes an English sentence ending with the character ".".

2. The characters that appear in the input English sentence include twenty-six uppercase English letters (ASCII code 65-90), twenty-six lowercase English letters (ASCII code 97-122), "," and ".", spaces character.
Input format:

An English sentence ending with the character ".".
Output format:

The output of the input English sentence after format error correction.
Input sample:

This is an Example with one mistake.

Sample output:

This is an example with one mistake.

Idea
Test these test points and it’s OK to
enter

i ajhas HjaHHAI,, ajj, I.
,,, sjhhhqHjGAHhIjjas,.
S, djojd doja kdaI.

Output (one-to-one correspondence with input)

I ajhas hjahhai, ajj, I
.,,, sjhhhqhjgahhijjas,.
S ,, djojd doja kdai.

key point
1, wherever there is a comma, a space must be added after it.
2 The first letter is capitalized
3. The i in the form of "i", "i, and "i." must be capitalized.
4 End with a period, and other characters are lowercase

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    string s1;
    getline(cin, s1);
    for(int i = 0; i < s1.size(); i++)
    {
    
    
        s1[i] = tolower(s1[i]);
        if(s1[i] == ',') // 在逗号后面加空格
            s1.insert( ++i, " ");
    }
    string s2 = s1;
    stringstream ss(s1); //流输出取消多个连续空格
    string ans = "";
    while(ss >> s1)
    {
    
    
        if(s1.size() == 1 && (s1[0] == ',' || s1[0] == '.'))//如果是标点
        {
    
    
            if(ans.size() == 0) ans = s1 + " ";//ans第一个字符不为空格
            else //ans中有单词了
            {
    
    
                int x3 = ans.size();
                if(!isalpha(ans[x3 - 2]))   ans = ans + s1 + " "; //如果上个单词为标点
                else ans.insert(x3 - 1, s1);//不为标点直接插入上个单词右侧
            }
        }
        else    ans = ans + s1 + " ";//单词直接相加
    }
    int x1 = 0;
    while(x1 != -1)
    {
    
    
        x1 = ans.find(" i", x1);
        if(x1 == -1)	break;
        if(!isalpha(ans[x1 + 2])) // 如果i后面是标点或者空格就变成大写
            ans[x1 + 1] = 'I';
        x1 ++;
    }
    ans[0] = toupper(ans[0]);//首字母大写
    ans.erase(ans.size() - 1); //去掉最后一个空格
    cout << ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45778406/article/details/109320135