I'm trying to work on a program that takes a string as an input, parses it into words according to spaces and newline characters, puts each word in its' own space into a char array, then prints the array. However, what I've got so far only prints garbage characters past the first two or so characters.
This:
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
char wordlist[100][100];
char templist[] = "This is a bunch of words";
char *tokenptr;
int wordcnt = 0;
tokenptr = strtok(templist, " ");
while (tokenptr != NULL)
{
if (tokenptr != " ");
{
*wordlist[wordcnt] = *tokenptr;
}
if (tokenptr == " ");
{
wordcnt++;
}
tokenptr = strtok(NULL, " ");
}
for (wordcnt = 0; wordcnt < 100; wordcnt++)
{
printf("%s", wordlist[wordcnt]);
}
}
outputs this:
Code:
Tiabowÿ=öÿ=ö¬ÿ=öÿ=ö ÿ?B@ÿ?@0ÿ;Á0ÿ¿ïÿ?@¸ÿ?B@ÿ?U°ÿ?@0ÿ¿ð,ÿ?{pÿ¿í¨
ÿ?Iÿ?{p¦)ãÿ2êøÿ¿îLÿ?@üÿ?ÿ3àPÿ5nÿ6ýÿ9 ÿ6²´ÿ?{pÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ;
Any idea what I'm doing wrong here?