I keep getting a segmentation error and i don't know why. Can you please help?
Thank you!
Here's the program:
insert
Code:
/*This program is suppose to take a sentence as input (in a string) and compute the number of words in the sentence.
*/
#include <stdio.h>
#include <string.h>
int main()
{
int i, leng, number_words;
char sentence[i];
char *sent = sentence;
char x;
// while (scanf("%s", sent) == 1)
// printf("%s\n", sent);
scanf("%[^\n]", sent);
printf("%s\n", sent);
leng = strlen(sentence);
printf("%d\n", leng);
number_words=1;
for(i=0; i<leng); i++)
{
if ((sentence[i] == ' ') || (sentence[i] == ','))
number_words = number_words + 1;
}
printf("the number of words is %d\n", number_words);
return 0;
}