•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1301 | Replies: 6
![]() |
| |
•
•
Join Date: Sep 2007
Posts: 21
Reputation:
Rep Power: 2
Solved Threads: 0
Hello,
Is there any way of reading words from a string containg a few words separated by white spaces using a loop? I don't know how many argumets the user will input. I wanted to do it like this:
But this keeps reading in the first word from the string.
Is there any way of reading words from a string containg a few words separated by white spaces using a loop? I don't know how many argumets the user will input. I wanted to do it like this:
for(i=0;i<wordCounter;i++)
{
sscanf(input,"%s",argument[i]);
}But this keeps reading in the first word from the string.
You can use the %n conversion specifier to find out how many characters were read and adjust the source string accordingly:
Note that the usual guideline of using the number of conversion specifiers to determine what value scanf should return doesn't apply here. %n doesn't add to the number of conversions, which is why the loop uses 1 instead of 2 to mark success.
#include <stdio.h>
int main( void )
{
const char *src = "this is a test";
char word[5];
int n;
while ( sscanf ( src, "%4s%n", word, &n ) == 1 ) {
puts ( word );
src += n;
}
return 0;
} Last edited by Narue : Oct 16th, 2007 at 5:03 pm.
I'm here to prove you wrong.
>Sorry, but I don't understand this code
Gone are the days when rookies would take a new piece of code and run off to dissect it.
>Why do you have to use puts(word) after reading in to word with sscanf?
Because it's generally a better example if you can see that it's working.
>And what does += do?
It does exactly what your book on C tells you it does: adds the value on the right to the value on the left.
Gone are the days when rookies would take a new piece of code and run off to dissect it.

>Why do you have to use puts(word) after reading in to word with sscanf?
Because it's generally a better example if you can see that it's working.
>And what does += do?
It does exactly what your book on C tells you it does: adds the value on the right to the value on the left.
I'm here to prove you wrong.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- C++ Strings (C++)
- File parsing in 'C' (C)
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C Forum
- Previous Thread: Can someone help me with my programming?
- Next Thread: Guide to proper code tagging using pictures.



Hybrid Mode