![]() |
|
#1
|
|||
|
|||
where am i going wrong?Hi,
I wrote following program to read a string from user containing words seperated by spaces and count the total no of words in the string and display each word separately .i.e.say a user entered string as "my name is wilson" the programs should output. total words:=4 word 1:=my word 2:=name word 3:=is word 4:=wilson following is my program.When I run it i get segmentation fault at the point mentioned in program. CPP / C++ / C Code:
Can some suggest what am doing wrong here ? Thanks, |
|
#2
|
||||
|
||||
|
Quote:
You have defined a buffer of pointers to tokens: CPP / C++ / C Code:
Then in the search function you use CPP / C++ / C Code:
__________________
Age is unimportant -- except in cheese |
|
#3
|
|||
|
|||
|
Hello waltp,
I declared it the following way and it worked. CPP / C++ / C Code:
Program gives expected output. my name is wilson Entered String is: my name is wilson Total No of words:= 4 Word[1]:= MY Word[2]:= NAME Word[3]:= IS Word[4]:= WILSON Thanks, |
|
#4
|
|||
|
|||
|
Without commenting on your style, here are my observations:
I see a couple of problems: one is obvious (and easy to fix); the other is extremely important, but maybe not so obvious. The first problem: if you have found four words, for example, then your loop prints out five words. Maybe you have fixed it already. If not, try changing CPP / C++ / C Code:
to CPP / C++ / C Code:
Now, for the important one: in your search() function, you have CPP / C++ / C Code:
In order to use strlen() properly, the character sequence must be null-terminated, and your input routine doesn't put the '\0' character in the buffer. The following works for me: upon exiting the while() loop, change CPP / C++ / C Code:
to CPP / C++ / C Code:
Dave |
|
#5
|
|||
|
|||
|
Quote:
Hi Dave, Yes,I have fixed the problem with additional string being displayed. And secondly,i use "\" to mark the end of the user entered string.Hence I read the string in search fucntion till i hit "\".So i actually dont need "\0" to end the string here.Anyway,thanks for the suggestion |
|
#6
|
|||
|
|||
|
Quote:
I can't tell you how important it is for you to understand that your use of strlen() is dangerous, since it is operating on something that is not a null-terminated sequence of characters. The function strlen() looks at memory, starting at the pointer value that you give it and goes until it sees a '\0'. So --- it might go into never-never land (or maybe not, depending on what happens to be in the memory at the time the program is run). Never-never land is famous for giving exception errors from time to time; sometimes the program appears to work OK, and sometimes it crashes. My point is: this is a Bad Thing to do, even if it looks like it works. Someday, somehow this kind of thing will leap up and bite you. As a matter of programming style, why did you use '\\' to terminate the string; why not just use '\0'? Then there's no question of using strlen() or any other standard library C functions. Regards, Dave |
Recent GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dropped By Google...What am I doing wrong? | ademaskoo | Search Engine Optimization Forum | 6 | 30-Jul-2004 23:08 |
| When server migrations go wrong... | Div | Web Hosting Forum | 7 | 12-Jul-2004 17:34 |
| What's wrong with this page? | Socrates Demise | Websites Reviewed Forum | 2 | 31-Mar-2004 09:50 |
| something wrong with this code | loon | MySQL / PHP Forum | 5 | 07-Jul-2003 05:55 |
| Thanx for the sql query, but there must be a little mistake | norok | MySQL / PHP Forum | 13 | 30-Jun-2003 06:30 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The