3 Replies - 1001 Views - Last Post: 22 December 2013 - 02:35 PM Rate Topic: -----

#1 ROFLN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-May 13

Reversing the order of words in a string

Posted 21 December 2013 - 10:12 PM

Hello,

I am currently on my winter break and I decided to brush up on some programming before the next semester begins (I've been only dealing with hardware for the past few months). So I looked in an old textbook and found a problem that I wasn't able to figure out in the past, so I decided to give it another go. The problem is to take in a string and then reverse the order of words. A hint was given that I should store the input in a character array, and then store the words of the string in an array of strings. Seemed easy at first, but something peculiar happened. When I ran my code to test to see if the words were actually being stored in the array of strings, I noticed that all of the preceding characters before the words (with exception of the first) were all null characters. I even followed the trace using the debugger on Devshed and there are just a bunch of null characters. Here is my code:

#include <stdio.h>
#include <ctype.h>

int main()
{
	char in[1023] = {0};
	char words[512][128] = {0};
	int start = 0;
	int end = 0;
	int i = 0;
	int wordcount = 0;
	int j = 0; 
	int k = 0; 
	
	//input
	printf("Enter a sentence: ");
	fgets(in, 1023, stdin);
	
	//word count (primarily for debugging)
	for(i = 0; in[i] != '\0'; i++)
	{
		if(in[i] == ' ' || in[i+1] == '\0')
		{
			wordcount++;
		}
	}
	
	printf("Word count: %d\n", wordcount);
	
	//find the individual words and store them in the array
	for(i = 0; in[i] != '\0'; i++)
	{
		int l = 0;
		
		if(in[i] == ' ')
		{
			end = i;
			
			for(k = start; k < end; k++)
			{
				words[j][l] = in[k];
				l++;
			}
			
			j++;
			start = end+1;
		}
		
		
		if(in[i+1] == '\0')
		{
			end = i+1;
			
			for(k = start; k < end; k++)
			{
				words[j][l] = in[k];
				l++;
			}
			break;
		}
			
	}
	
	//display contents of words array
	for(i = 0; i < wordcount; i++)
	{
		for(j=0; j < 128; j++)
		{
			printf("%c", words[i][j]);
		}
	}

	return(0);
}



Once I can get this sorted out, I am definitely sure I can print out what is desired.

Is This A Good Question/Topic? 0
  • +

Replies To: Reversing the order of words in a string

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: Reversing the order of words in a string

Posted 21 December 2013 - 10:48 PM

> 41 words[j][l] = in[k];
You should arrange for each of these string copying loops to append a \0 to the end of the string when the loop is done.

> 67 for(j=0; j < 128; j++)
You're printing a load of garbage (or nul's) because you're going way past the valid data.

> 69 printf("%c", words[i][j]);
If you make them proper strings (point 1), then you can dispense with the inner loop and do
printf("%s\n", words[i]);
Was This Post Helpful? 1
  • +
  • -

#3 ROFLN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-May 13

Re: Reversing the order of words in a string

Posted 22 December 2013 - 09:44 AM

So, I made the modifications that you suggested and it seems to store the words correctly for the most part but it tags on a zero. But this only occurs when I put in the line to append a null character at the end of the word. Without it, it stores without the zero. Also, when it prints a space appears before the last (now first) word. I appreciate your help, by the way.

Here's the command prompt output:

Quote

reverse.c: In function 'main':
reverse.c:7:2: warning: missing braces around initializer [-Wmissing-braces]
reverse.c:7:2: warning: (near initialization for 'words[0]') [-Wmissing-braces]
reverse.c:59:18: warning: multi-character character constant [-Wmultichar]
reverse.c:59:4: warning: overflow in implicit constant conversion [-Woverflow]

C:\Users\Nick\Desktop\Projects\Retry>reverse
Enter a sentence: testing one two three
Word count: 4
testing
one
two
three0
three0 two one testing


//find the individual words and store them in the array
	for(i = 0; in[i] != '\0'; i++)
	{
		int l = 0;
		
		if(in[i] == ' ')
		{
			end = i;
			
			for(k = start; k < end; k++)
			{
				words[j][l] = in[k];
				l++;
			}
			
			j++;
			start = end+1;
		}
		
		
		if(in[i+1] == '\n')
		{
			end = i+1;
			
			for(k = start; k < end; k++)
			{
				words[j][l] = in[k];
				l++;
			}
			words[j][l] = '/0';
		}
			
	}
	
	//display contents of words array
	for(i = 0; i < wordcount; i++)
	{
		printf("%s\n", words[i]);
	}

	for(i = wordcount; i >=0; i--)
	{
		printf("%s ", words[i]);
	}
	
	return(0);
}


Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: Reversing the order of words in a string

Posted 22 December 2013 - 02:35 PM

> 30 words[j][l] = '/0';
/0 is not the same thing as \0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1