GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Aug-2004, 14:16
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

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:
#include <stdio.h>
#include <ctype.h>

void search (char *str,char *tokens[]);
char *tokens[100]={"\0"};
int main()
{
	int i,j,k,count,total;
	char ch1,ch2;
	char list[200];
	i=0;
	printf("\n\nEnter the string with words seperated by white space now...\n\n");
	while ((ch1=getchar())>=0)
	{
		list[i]=ch1;
		i++;
	}
	list[i]='\\';
	count=0;
	total=0;
	printf("\n");
	search(list,tokens);
	return 0;
}

void search (char str[],char *tokens[])
{
	int i,j,count,total,wordcount;
	total=wordcount=count=0;
	for (j=0;j<(strlen(str));j++)
	{
		if (!(isspace(str[j])) && !(str[j] == '\\'))
		{
			/*printf("total= %d\tcount:= %d\t",total,count);
			printf("Str[%d]:= %c\n",j,str[j]);*/
			tokens[total][count]=str[j];			// Segmentation Fault at this point works fine if i comment this with above 2 printfs.
			/*printf("%c",tokens[total][count]);*/
			count++;
		} else {
			count=0;
			if ((isspace(str[j-1])) && str[j] == '\\')
			{
				break;
			} else {
			total++;
			}
		}
	}
	printf("\ntotal in function:= %d\n",total);
	
	for (i=0;i<=total;i++)
	{
		printf("Word[%d]:= ",i+1);
		for (j=0;j<(strlen(tokens[i]));j++)
		{
			tokens[i][j]=toupper(tokens[i][j]);
			printf("%c",tokens[i][j]);
		}
		printf("\n");
	}
}

Can some suggest what am doing wrong here ?

Thanks,
  #2  
Old 01-Aug-2004, 19:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
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.

Can some suggest what am doing wrong here ?

Thanks,

You have defined a buffer of pointers to tokens:
CPP / C++ / C Code:
char *tokens[100]={"\0"};
all pointing nowhere.

Then in the search function you use
CPP / C++ / C Code:
tokens[total][count] = str[j];
but there is no space defined at tokens[total][count] so you are placing str[j] in the middle of nowhere. And that causes your seg fault.
__________________

Age is unimportant -- except in cheese
  #3  
Old 02-Aug-2004, 11:38
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hello waltp,

I declared it the following way and it worked.

CPP / C++ / C Code:
char tokens[25][100];

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  
Old 02-Aug-2004, 14:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
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:
 

  for (i=0;i<=total;i++)


to

CPP / C++ / C Code:

  for (i=0; i < total; i++)
  

Now, for the important one: in your search() function, you have

CPP / C++ / C Code:
 
  for (j=0;j<(strlen(str));j++)
  

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:
  list[i]='\\';
  count=0;

to

CPP / C++ / C Code:
  
  list[i]='\\';
  list[i+i] = '\0';
  count=0;



Dave
  #5  
Old 03-Aug-2004, 02:22
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by davekw7x
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:
 

  for (i=0;i<=total;i++)


to

CPP / C++ / C Code:

  for (i=0; i < total; i++)
  

Now, for the important one: in your search() function, you have

CPP / C++ / C Code:
 
  for (j=0;j<(strlen(str));j++)
  

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:
  list[i]='\\';
  count=0;

to

CPP / C++ / C Code:
  
  list[i]='\\';
  list[i+i] = '\0';
  count=0;



Dave

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  
Old 03-Aug-2004, 07:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
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



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 GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.