Need help with string manipulation and seg faults

Reply

Join Date: Mar 2007
Posts: 3
Reputation: Marauder_Pilot is an unknown quantity at this point 
Solved Threads: 0
Marauder_Pilot Marauder_Pilot is offline Offline
Newbie Poster

Need help with string manipulation and seg faults

 
0
  #1
Mar 7th, 2007
Alright, here's what I'm working on now. It's supposed to go through an inputted list of words, pick out actual words and create a numbered list of them, then, where it finds a number in the list, go back in the list from that number, replace it with a word, augment the list accordingly and continue.

Anyways, here's what I have:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. int main ()
  7. {
  8. char line[100];
  9. char word[100];
  10. char tword[20];
  11.  
  12. int k = 0, index = 0, nwords = 0, j;
  13.  
  14. gets(line);
  15.  
  16. while(line[0] != '0')
  17. {
  18. if(isalpha(line[k]))
  19. {
  20. j = 0;
  21. while (isalpha(line[k]))
  22. {
  23. tword[j] = line[k];
  24. j++;
  25. k++;
  26. }
  27. tword[j] = '\0';
  28. strcpy(word[nwords], tword);
  29. nwords++;
  30. }
  31. else if(isdigit(line[k]))
  32. {
  33. index = atoi(line[k]);
  34. k++;
  35. if(isdigit(line[k]))
  36. {
  37. index = index * 10 + atoi(line[k]);
  38. k++;
  39. }
  40. printf("%s", word[nwords - index]);
  41. strcpy(tword, word[nwords-index]);
  42. for(j = index + 1; j < nwords; j++)
  43. {
  44. strcpy(word[j - 1], word[j]);
  45. }
  46. strcpy(word[nwords - 1], tword);
  47. }
  48. else
  49. {
  50. printf("%c", line[k]);
  51. k++;
  52. }
  53. gets(line);
  54. }
  55. return 0;
  56. }
The problem is that I get segmentation faults when executing. Anybody see what I'm missing?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Need help with string manipulation and seg faults

 
0
  #2
Mar 7th, 2007
Several problems in your code.
  • gets has no bounds-checking when it gets input, instead you should use fgets.
  • This code:
    1. strcpy(word[nwords], tword);
    This is copying an entire string into 1 character of the string. Use 2d array of chars if you want to have an array of strings.
  • strtol might be a better function to use instead of atoi if you wish to perform error-checking number conversion.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: Marauder_Pilot is an unknown quantity at this point 
Solved Threads: 0
Marauder_Pilot Marauder_Pilot is offline Offline
Newbie Poster

Re: Need help with string manipulation and seg faults

 
0
  #3
Mar 7th, 2007
Originally Posted by joeprogrammer View Post
Several problems in your code.
  • gets has no bounds-checking when it gets input, instead you should use fgets.
  • This code:
    1. strcpy(word[nwords], tword);
    This is copying an entire string into 1 character of the string. Use 2d array of chars if you want to have an array of strings.
I implimented both of those, but to no avail. Here's the new version:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. int main ()
  7. {
  8. char line[100];
  9. char word[20][100];
  10. char tword[20];
  11.  
  12. char *linePtr;
  13.  
  14. int k = 0, index = 0, nwords = 0, j;
  15.  
  16. fgets(line, 100, stdin);
  17.  
  18. while(line[0] != '0')
  19. {
  20. if(isalpha(line[k]))
  21. {
  22. j = 0;
  23. while (isalpha(line[k]))
  24. {
  25. tword[j] = line[k];
  26. j++;
  27. k++;
  28. }
  29. tword[j] = '\0';
  30. strcpy(word[nwords], tword);
  31. nwords++;
  32. }
  33. else if(isdigit(line[k]))
  34. {
  35. *linePtr = line[k];
  36. index = atoi(line[k]);
  37. k++;
  38. if(isdigit(line[k]))
  39. {
  40. index = index * 10 + atoi(line[k]);
  41. k++;
  42. }
  43. printf("%s", word[nwords - index]);
  44. strcpy(tword, word[nwords-index]);
  45. for(j = index + 1; j < nwords; j++)
  46. {
  47. strcpy(word[j - 1], word[j]);
  48. }
  49. strcpy(word[nwords - 1], tword);
  50. }
  51. else
  52. {
  53. printf("%c", line[k]);
  54. k++;
  55. }
  56. }
  57. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Need help with string manipulation and seg faults

 
0
  #4
Mar 7th, 2007
I'm surprised your code even compiles (it doesn't actually, I just tried compiling it with gcc and it failed with several syntax errors).

Why are you looping until the first character of line is 0? My only guess is that the list of words is terminated with a 0, but then you'd have to use a variable to keep track of the iterations, and then use this as your array subscript.

And why do you keep a separate variable tword for copying the strings? Save yourself a strcpy call and just copy it directly into the string array.

Your number extraction is totally screwed. You only feed a single char to atoi. Giving atoi a single char not only results in a compiler error, but there could in fact be more than 1 digit in the number. Try creating a loop that goes until it encounters a non-numeric character, and then feed the string obtained back to atoi.

And finally, why is the printf() statement in the word extraction loop? Create a separate loop after the extractions are all said and done, and you might actually get some console output!
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Need help with string manipulation and seg faults

 
0
  #5
Mar 8th, 2007
Hope this is not what you posted it for:
test.cpp(43) : warning C4700: local variable 'linePtr' used without having been initialized

Line 43 in my test.cpp is "*linePtr = line[k];"
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Need help with string manipulation and seg faults

 
0
  #6
Mar 8th, 2007
I dont see the login behind this code. You have been all around. Why do u want to loop around with the string just to copy when u have a routines to do that.

What is that u wanted, explain it properly.

ssharish2005
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: underjack is an unknown quantity at this point 
Solved Threads: 2
underjack underjack is offline Offline
Newbie Poster

Re: Need help with string manipulation and seg faults

 
0
  #7
Mar 8th, 2007
Here's how I interperted your problem:
Given a list of Words and Numbers:
--Make a numbered list of the words.
--Replace numbers with words from the list.

Instead of a console program, I wrote a program that takes a filename at command line. I expect one word or number per line, and output to the second filename, or the first filename +".new" if only one argument is given.

Other than the file aspect, the algorithm should be pretty straight forward. I used an array of arrays for the strings, and kept how many word are in it with a variable. Max size is hard coded at 4095 words. I have one function to check the array for a word, it returns -1 if it is not found.

This compiles and runs without errors or warnings under Visual C++ 6, should be fine under other compilers.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. int find_word(const char ** array, const int size, const char * item)
  7. {
  8.  
  9. int i = 0;
  10.  
  11. while (i < size)
  12. {
  13. if (strcmp(array[i], item) == 0)
  14. return i;
  15. else
  16. i++;
  17. }
  18.  
  19. return -1;
  20.  
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.  
  26. char filename[65536], outputfilename[65536];
  27. FILE * file;
  28. FILE * outputfile;
  29. char buffer[256];
  30. char * array[4096];
  31. int size = 0;
  32. int pos;
  33.  
  34. if (argc == 1)
  35. {
  36. printf("Not enought parameters!\n");
  37. return 1;
  38. }
  39.  
  40. if (argc > 1)
  41. {
  42. strcpy(filename, argv[1]);
  43. }
  44.  
  45. if (argc > 2)
  46. {
  47. strcpy(outputfilename, argv[2]);
  48. }
  49. else
  50. {
  51. strcpy(outputfilename, filename);
  52. strcat(outputfilename, ".new");
  53. }
  54.  
  55. file = fopen(filename, "r");
  56. outputfile = fopen(outputfilename, "w");
  57.  
  58. if ((file == NULL) || (outputfile == NULL))
  59. {
  60. printf("Error opening file\n");
  61. return 2;
  62. }
  63.  
  64. while(!feof(file))
  65. {
  66. if (fgets(buffer, 256, file) == NULL) break;
  67.  
  68. pos = strlen(buffer) - 1;
  69. if (buffer[pos] == '\n') buffer[pos] = 0; // remove trailing LFCR's
  70.  
  71. if (isalpha(buffer[0])) // word
  72. {
  73. pos = find_word(array, size, buffer);
  74. if (pos == -1) // word is not in the array, must add it.
  75. {
  76. if (size == 4095) exit(3); // size check for array
  77.  
  78. array[size] = (char *) malloc(256 * sizeof(char));
  79. if (array[size] == NULL) exit(3); // check malloc()!!
  80. strcpy(array[size], buffer);
  81. printf("%s added at %i\n", buffer, size);
  82. size++;
  83. }
  84. // nothing else, do nothing on duplicate words
  85. } // end of if -- isalpha
  86. else // word is already in the array!
  87. {
  88. pos = atoi(buffer);
  89. if (pos >= size)
  90. {
  91. printf("Error! %d not in the array!!\n", pos);
  92. }
  93. else
  94. {
  95. strcpy(buffer, array[pos]);
  96. printf("Replacing %d with %s\n", pos, buffer);
  97. }
  98. }
  99.  
  100. fputs(buffer, outputfile);
  101. fprintf(outputfile,"\n");
  102.  
  103. }
  104.  
  105. fclose(file);
  106. fclose(outputfile);
  107.  
  108. }

Hope that answers your problem. I found it easier to assume what you were trying to do it, and show how I would do it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC