Question: When you enter a string with scanf, if you encounter a space in the input, scanf puts the characters before them into an array of characters that follow. So what if the requirement is not to use a space as the end of the input, but to end the input string with the last carriage return? I have also seen in some BBS or forums that people have asked this question, the general answer is to use gets (). In fact, scanf can do this function completely. The statement is like this: Scnaf ("%[^/n]s", str); Now, let's talk about this feature in detail. First, this feature is for%s, between% and s can be inserted [], the inside bracket is a character set, if the letter in the input string in this character set, will be read in, when the first letter is not in the character set, the input will end. For example: scanf ("%[abc]s", str); Input is: abccbadef input is: ABCCBA Second, there are two special characters in the brackets: ^ and-^, which means that the letters that do not appear in the character set are accepted. It must appear immediately after [to have this function, otherwise it will be treated as a generic character. In the above example, [^/n] means that the character entered will be accepted as long as the carriage return is not encountered. -Represents a connector, such as 0-9 A-Z, which represents a range in which characters are accepted or not accepted. If you want to treat-as a general character-it must appear immediately before the. In addition, if the latter bracket is to be used as a general character, it must appear immediately after the [or ^ 's] back. The use of scanf can be found in MSDN, but it is not obvious that there are at least 3 links to get to that page, and just probably said, there is no detailed explanation. There is a detailed explanation for this problem on the Linux man page, and if you are interested you can go and see it. Supplement, this is I in the previous stage by chance to see the article, excerpt down, forget to remember the source ... Today to organize notes, decided to paste on the blog and share with you, if the author saw no details of the source, forget to forgive.
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.