C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2009, 05:57 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 1
Debugging strings program help plz

Hi, I'm having some problems with this program. The program is suppose to ask for an input sentence and print the order of the words in reverse. The program complies, but crashes...not sure why.
Thanks,

Example run:
Enter a sentence: you can cage a swallow can't you
Reversal of sentence: you can't swallow a cage can you


Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_LEN 80
#define MAX_WORDS 10

int get_words (char *line, char tokens[MAX_WORDS][MAX_LEN+1]);
void replace_whitespace (char *str);

int main(void)
{ 
  int i;
  char *str, tokens[MAX_WORDS][MAX_LEN+1];

  printf("Enter a sentence: ");
  gets(str);

  get_words(str, tokens);

  printf("Reversal  of sentence: ");

  for (i = strlen(str); i > 0; i--)
    printf("%s ", str[i]);

return 0;
}

/* store the words from string line in the array 
         tokens, one word per entry. */

int get_words (char *line, char tokens[MAX_WORDS][MAX_LEN+1])
{
  int i, char_count = 0;
  char *p = line;
  int len;

  replace_whitespace(p);

  len = strlen(p);
  
  while (*p == '\0' && *p < len) 
  {
     p++;
     char_count++;
  }
  
  while (*p != '\0')
  {  
     strcpy(tokens[i], p);
     p += strlen(p);
     while(*p == '\0')
        p++;  
     i++;
  }


}

/* replaces every whitespace character in str with the null 
   character; uses the function isspace(char c) from ctype.h */

void replace_whitespace (char *str)  
{  
   int i;

   for (i = 0; i < strlen(str); i++)
     if (isspace(str[i]))
       str[i] = '\0';
        
}
allen9190 is offline   Reply With Quote
Old 11-08-2009, 08:17 PM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_LEN 80
#define MAX_WORDS 10

int get_words (char *line, char tokens[MAX_WORDS][MAX_LEN+1]);
void replace_whitespace (char *str);

int main(void)
{ 
  int i;
  char *str, "str" doesn't point anywhere useful.
          tokens[MAX_WORDS][MAX_LEN+1];

  printf("Enter a sentence: ");
  gets(str);  gets() is dangerous, due to buffer overruns -- see the FAQ, use fgets() instead. See above, "str" does not point anywhere. 

  get_words(str, tokens);

  printf("Reversal  of sentence: ");

  for (i = strlen(str); i > 0; i--)
    printf("%s ", str[i]);

return 0;
}

/* store the words from string line in the array 
         tokens, one word per entry. */

int get_words (char *line, char tokens[MAX_WORDS][MAX_LEN+1])
{
  int i, char_count = 0;
  char *p = line;
  int len;

  replace_whitespace(p);  This is incorrect logic, how do you know where the last whitespace character was replaced?

  len = strlen(p);  This will give you the length of the first part (upto the first whitespace character). 
  
  while (*p == '\0' && *p < len) 
  {
     p++;
     char_count++;
  }
  
  while (*p != '\0')
  {  
     strcpy(tokens[i], p);
     p += strlen(p);
     while(*p == '\0')
        p++;  
     i++;
  }


}

/* replaces every whitespace character in str with the null 
   character; uses the function isspace(char c) from ctype.h */

void replace_whitespace (char *str)  
{  
   int i;

   By replacing whitespace with null terminators, you're changing the length of the string, thus this loop is useless. It will halt on the first whitespace character.
   for (i = 0; i < strlen(str); i++)
     if (isspace(str[i]))
       str[i] = '\0';
        
}
Do you have to reverse the string in-place?
Even so, why not just replace the whitespace with null terminators and have an array of pointers to each of the tokens (in your array).

What I mean is:
Code:
zac@neux:code (1) $ gcc rev.c -o rev
zac@neux:code (1) $ ./rev 
Token 1: This
Token 2: is
Token 3: an
Token 4: example
zac@neux:code (1) $ cat rev.c 
#include <stdio.h>

#define MAX_WORDS 4

int main(void)
{
   char str[] = "This is an example";
   char * tokens[MAX_WORDS] = {NULL};
   size_t i = 0;

   size_t numberOfWords = 4;        /* obviously you'd have to calculate this, and below at runtime */

   tokens[0] = str;           /* This */
   str[4] = '\0';

   tokens[1] = (str + 5);     /* is */
   str[7] = '\0';

   tokens[2] = (str + 8);     /* an */
   str[10] = '\0';

   tokens[3] = (str + 11);    /* example */

   for(i = 0; i < numberOfWords; ++i)
   {
      printf("Token %d: %s\n", i + 1, tokens[i]);
   }

   return 0;
}
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 11-08-2009 at 08:31 PM.
zacs7 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can any1 plz make this assignment jean C Programming 17 05-13-2009 09:19 PM
need help checking char strings plz dezz101 C Programming 18 09-10-2008 11:47 PM
plz can u help me with this program itcs C Programming 6 07-31-2003 08:56 AM
Can someone help me plz Shadow C Programming 5 02-15-2002 01:41 PM
plz hlp me. program not running properly jfl C Programming 5 02-11-2002 03:58 PM


All times are GMT -6. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22