Sscanf () function usage
SSCANF usage: (a better function discovered after qsort, bsearch, and strchr)
Similar to scanf, sscanf is used for input, but the latter uses the keyboard (stdin) as the input source, and the former uses a fixed string as the input source.
Example:
1. Common usage.
Char buf [512];
Sscanf ("123456", "% s", buf); // here buf is the array name, which means to store 123456 in the form of % s into buf!
Printf ("% s \ n", buf );
Result: 123456
2. Take a string of the specified length. In the following example, a string with a maximum length of 4 bytes is obtained.
Sscanf ("123456", "% 4 s", buf );
Printf ("% s \ n", buf );
Result: 1234
3. Obtain the string of the specified character. For example, in the following example, the string is obtained when a space is encountered.
Sscanf ("123456 abcdedf", "% [^]", buf );
Printf ("% s \ n", buf );
Result: 123456
4. Take a string that only contains the specified character set. For example, in the following example, take a string that only contains 1 to 9 letters and lowercase letters.
Sscanf ("123456 abcdedfBCDEF", "% [1-9a-z]", buf );
Printf ("% s \ n", buf );
Result: 123456 abcdedf
When input:
Sscanf ("123456 abcdedfBCDEF", "% [1-9A-Z]", buf );
Printf ("% s \ n", buf );
Result: 123456
5. Obtain the string of the specified character set. For example, in the following example, a string with uppercase letters is used.
Sscanf ("123456 abcdedfBCDEF", "% [^ A-Z]", buf );
Printf ("% s \ n", buf );
Result: 123456 abcdedf
6. Given a string iios/12DDWDFF @ 122, get the string between/and @ and filter out "iios/" first, then, send a string of content other than '@' to the buf.
Sscanf ("iios/12DDWDFF @ 122", "% * [^/]/% [^ @]", buf );
Printf ("% s \ n", buf );
Result: 12 DDWDFF.
7. Given a string "hello, world", only world is retained. (Note: There is a space after)
Sscanf ("hello, world", "% * s % s", buf );
Printf ("% s \ n", buf );
Result: world
% * S indicates that the first matching % s is filtered out, that is, hello is filtered out.
If there is no space, the result is NULL.
The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
Char word [1000];
Char arr [100] [100]; // arr is used to store previous words.
Int main ()
{
Int len, pos;
Char temp [100];
While (gets (word) & strcmp (word ,"#")! = 0) // The question requirement is not at the end of the article. Note that "#" is used for strcmp comparison.
{
Len = strlen (word); // The total length (including spaces)
Pos = 0;
Int cnt = 0;
// Add the word length to the pos until> len
While (pos <= len)
{
Sscanf (word + pos, "% s", temp); // save a word to temp and spaces (sscanf should read the space deadline, which is important for sscanf)
// Word + pos is the pointer pointing position
// Printf ("% s ++", temp );
Int k;
For (k = 0; k <cnt; ++ k)
If (strcmp (temp, arr [k]) = 0) // if it is the same as the words previously saved, no count
Break;
If (k = cnt)
Strcpy (arr [cnt ++], temp); // save temp to arr and add one to the counter cnt
Pos + = strlen (temp) + 1; // + 1 indicates adding spaces. The last pos must be greater than len
}
Printf ("% d \ n", cnt );
}
Return 0;
}
Original translated from: http://minda-ly.diandian.com/post/2011-02-13/17223519