•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,563 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 2,728 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:
Views: 1010 | Replies: 6
![]() |
•
•
Join Date: Sep 2007
Posts: 20
Reputation:
Rep Power: 1
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 4:03 pm.
Member of: Beautiful Code Club.
>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.
Member of: Beautiful Code Club.
Sounds like bad copying on your part, replacing a pointer with an array.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
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.



Linear Mode