![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 27
| 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;
}
|
| kuljule is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Code: int i, leng, number_words;
char sentence[i];
Thus when you write to it, segfault because you probably go past the end -- who knows! Oh and don't use scanf for strings. Use fgets() -- see the FAQ. Last edited by zacs7; 11-18-2008 at 10:59 PM. |
| zacs7 is offline | |
| | #3 |
| Registered User Join Date: Nov 2008
Posts: 27
| What do you mean i is not initialized? Isn't it initialized in the for loop? Also, I here bad talk about using fgets. Why do people go against using it? What's the difference between fgets and scanf of a string? |
| kuljule is offline | |
| | #4 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| No, you use i when you declare 'something'. http://cboard.cprogramming.com/showp...37&postcount=9 There is nothing wrong with using fgets(), people against using it probably have no idea why they're against it. The only bad thing is it reads in the newline (that's not bad), you can remove the newline -- see the FAQ. Last edited by zacs7; 11-18-2008 at 11:10 PM. |
| zacs7 is offline | |
| | #5 |
| Registered User Join Date: Nov 2008
Posts: 27
| using gets(sent) doesn't change the segmentation error. But how do I initialize i? |
| kuljule is offline | |
| | #6 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Don't use gets() -- see the FAQ. > But how do I initialize i? Oh I don't know, Code: int i = 50; But that's only half the problem. Perhaps read the first few chapters of a good C programming book... or the tutorials on this site. Is this for CSCI 261 class (Computer Science 1)? If so, some of your "buddies" have asked the same question in the last few days. |
| zacs7 is offline | |
| | #7 |
| Registered User Join Date: Nov 2008
Posts: 27
| Its C Programming. That's the class. The thing about initializing it means that it won't work if its greater than that number. I want to be able to do this with any length of a string. I think the segmentation error is coming from leng = strlen(sentence); printf("%d\n", leng); what's the problem with this? |
| kuljule is offline | |
| | #8 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Nothing. If you're not going to listen, I won't bother. I've told you where the segfault is coming from, FIX IT. Ignoring the pointless pointer, Consider this, Code: #define USELESS 5
char sentence[USELESS];
scanf("%s", sentence);
Now consider: Code: char sentence[256]; /* magic number, perhaps define this somewhere. Or use BUFSIZ (look it up) */ /* TODO: check the return result of fgets() */ fgets(sentence, sizeof(sentence), stdin); Last edited by zacs7; 11-18-2008 at 11:30 PM. |
| zacs7 is offline | |
| | #9 |
| Registered User Join Date: Nov 2008
Posts: 27
| I'm sorry if I am seeming like I'm not listening. I am, but I am just really confused. Can you repeat where the seg fault is coming from? The program prints out the sentence but then after printing it out, it says seg fault. |
| kuljule is offline | |
| | #10 |
| Registered User Join Date: Nov 2008
Posts: 27
| nevermind figured it out. Thanks! |
| kuljule is offline | |
| | #11 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Say your random-length sentence was 5 chars, and then you read in 20 chars. Where do the other 15 go - answer, over someone elses memory. When do you get the segfault, when that someone else comes back to find the values they thought were safe have in fact been trashed by your sloppy code.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| program, word count |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Avoiding Global variables | csonx_p | Windows Programming | 32 | 05-19-2008 12:17 AM |
| brace-enclosed error | jdc18 | C++ Programming | 53 | 05-03-2007 05:49 PM |
| First Time Using Linked Lists, Having Trouble Replacing First Node | Peter5897 | C Programming | 7 | 06-16-2006 09:23 PM |
| load gif into program | willc0de4food | Windows Programming | 14 | 01-11-2006 10:43 AM |
| Why wont my function exit correctly? | LightsOut06 | C Programming | 2 | 10-09-2005 09:23 PM |