Use scanf to implement the gets Function

Source: Internet
Author: User
Question: When Using scanf to input a string, if a space is encountered in the input, scanf will put the character before the space into the character array behind it. So what should we do if we do not want to end the input with space, but with the last carriage return as the end of the input string? I have also seen this question in some BBs or forums. Generally, the answer is gets (). In fact, scanf can fully implement this function. The statement is as follows: scnaf ("% [^/n] S", STR); this function is described in detail now. First, this function is for % S. you can insert [] between % and S. The brackets contain a character set. If the letters in the input string are in this character set, it will be read in. When the first letter is not in this character set, the input will end. For example: scanf ("% [ABC] S", STR); input: abccbadef; input: abccba; two special characters in brackets: ^ and-^ indicate none, the letters in this character set cannot be accepted. This function must appear immediately after [, otherwise it will be treated as a general character. In the above example, [^/n] indicates that as long as the carriage return is not met, the entered characters will be accepted. -Indicates a connector, for example, 0-9 A-Z. It indicates a range. All characters in this range are accepted or not accepted. If you want to treat "-" as a general character, "-" must appear immediately before. In addition, if you want to use parentheses as common characters, they must appear immediately after [or ^. The usage of scanf can be found in msdn, but it is not very obvious. At least three links can be found on that page, and I just gave a rough description and didn't explain it in detail. I have a detailed explanation of this problem on the Linux man page. If you are interested, go and check it out. In addition, this is an article that I accidentally saw in the previous stage. I excerpted it and forgot to remember the source ...... Today, I decided to post my notes to my blog and share them with you. If the author does not specify the detailed source, I am sorry!

 

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.