Is this possible? I would like to have a user input a sentence, and then have each word in that sentence be put into its own array.
Loopah 0 Junior Poster in Training
Recommended Answers
Jump to PostPost your attempt. The basics are something like this:
static const char delim[] = " \n"; size_t size = 0; char word[10][20], *ptr = strtok(sentence, delim); while ( ptr != NULL ) { strcpy(word[size], ptr); if ( ++size >= sizeof word / sizeof *word ) { break; …
Jump to PostFrom my earlier mockup:
#include <stdio.h> #include <string.h> int main(void) { char sentence[200]; puts("enter a sentence:"); fflush(stdout); if ( fgets(sentence, sizeof sentence, stdin) != NULL ) { static const char delim[] = " \n"; size_t i, size = 0; char word[10][20], *ptr = strtok(sentence, delim); while ( …
Jump to PostFirst I think you need to obtain user input more cleanly. To that end, scanf is not your friend.
Jump to PostHmm, that is they way we were always taught to get input. Not sure of another way to get input. That way has always worked in the past.
Well, there is lots of bad information out there and plenty of bad teachers.
All 16 Replies
WolfPack 491 Posting Virtuoso Team Colleague
SpS 34 Posting Pro
Loopah 0 Junior Poster in Training
SpS 34 Posting Pro
Loopah 0 Junior Poster in Training
Dave Sinkula 2,398 long time no c Team Colleague
Loopah 0 Junior Poster in Training
Loopah 0 Junior Poster in Training
Dave Sinkula 2,398 long time no c Team Colleague
Loopah 0 Junior Poster in Training
Dave Sinkula 2,398 long time no c Team Colleague
Loopah 0 Junior Poster in Training
Dave Sinkula 2,398 long time no c Team Colleague
Loopah 0 Junior Poster in Training
Dave Sinkula 2,398 long time no c Team Colleague
Loopah 0 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.