FZU1054 reading order & C + + GetChar and gets functions parsing __ function

Source: Internet
Author: User


http://acm.fzu.edu.cn/problem.php?pid=1054





This is a reverse string problem.

Ideas:

1. Number of input strings

2. Get the input string

3. Reverse, output.


The final AC code is as follows

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main () {
    int number;
    Char ch[220];
    cin>>number;
    GetChar ();
    while (number--) {
        gets (CH);
        int len = strlen (ch);
        for (int i = len-1;i>= 0;i--) {
            cout<<ch[i];
        }
        cout<<endl;
    }
    return 0;
}

Compare the following code





The difference between the two pieces of code is that the position of the GetChar is different, but only the first one is AC.

Here is the difference between the two functions that do not understand GetChar and gets.

Gets

Receives a string of characters from the standard input, encounters ' \ n ' when it ends, but does not receive ' \ n ', holds ' \ n ' the input buffer, stores the received string of characters in the space that the form parameter pointer points to, and automatically adds a ' ".
GetChar

Receives a character return from the standard input, leaving the extra characters all in the input buffer.


In the title, the number only needs to be entered once, so use the GetChar, while the while loop will enter the string multiple times, so use gets.

So the GetChar in the while loop is obviously the wrong logic.



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.