What I'm trying to do is, given a string (say "This is a string") write a code that could take it and put each word in a separate element of an array ("This", "is", "a", "string"). I scribbled something like that:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='\0') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
Unfortunately, it doesn't act as needed (printing out some gibberish). I tried debugging it but I still don't understand why it wouldn't function properly. Guidance would be very helpful :) Also - if you have a more efficient idea, say one involving pointers, I would be more than thrilled to see it.
For starters this call of scanf
scanf("%s", bigString);
is unsafe and can read only one word.
Instead you can use standard function fgets
.
If a space character is encountered then this while loop
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
is skipped. In this case an element with the index i
of the two-dimensional character array will not be filled by a string. As a result this call of printf
printf("%s", wordCollection[i]);
invokes undefined behavior. And moreover if there are several adjacent spaces in the record and the number of words in the string is even less than 10 it can occur such a way that neither word will be outputted.
Also you need to append each element of the array wordCollection
that was filled with the terminating zero character '\0'
to make a string.
And this pair of statements
if (bigString[bigIndex]=='\0') i=10;
//...
printf("%s", wordCollection[i]);
again invokes undefined behavior because 10 is an invalid index.
Instead of the manually written while loop you could use standard C string functions.
And pay attention to that in general words can be separated by several adjacent spaces of tab characters.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
The program output is
This is a string
This
is
a
string