| | |
Need help with string manipulation and seg faults
![]() |
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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:
The problem is that I get segmentation faults when executing. Anybody see what I'm missing?
Anyways, here's what I have:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main () { char line[100]; char word[100]; char tword[20]; int k = 0, index = 0, nwords = 0, j; gets(line); while(line[0] != '0') { if(isalpha(line[k])) { j = 0; while (isalpha(line[k])) { tword[j] = line[k]; j++; k++; } tword[j] = '\0'; strcpy(word[nwords], tword); nwords++; } else if(isdigit(line[k])) { index = atoi(line[k]); k++; if(isdigit(line[k])) { index = index * 10 + atoi(line[k]); k++; } printf("%s", word[nwords - index]); strcpy(tword, word[nwords-index]); for(j = index + 1; j < nwords; j++) { strcpy(word[j - 1], word[j]); } strcpy(word[nwords - 1], tword); } else { printf("%c", line[k]); k++; } gets(line); } return 0; }
Several problems in your code.
getshas no bounds-checking when it gets input, instead you should usefgets.
- This code: 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.C Syntax (Toggle Plain Text)
- strcpy(word[nwords], tword);
strtolmight be a better function to use instead ofatoiif you wish to perform error-checking number conversion.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Several problems in your code.
getshas no bounds-checking when it gets input, instead you should usefgets.
- This code:
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.C Syntax (Toggle Plain Text)
strcpy(word[nwords], tword);
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main () { char line[100]; char word[20][100]; char tword[20]; char *linePtr; int k = 0, index = 0, nwords = 0, j; fgets(line, 100, stdin); while(line[0] != '0') { if(isalpha(line[k])) { j = 0; while (isalpha(line[k])) { tword[j] = line[k]; j++; k++; } tword[j] = '\0'; strcpy(word[nwords], tword); nwords++; } else if(isdigit(line[k])) { *linePtr = line[k]; index = atoi(line[k]); k++; if(isdigit(line[k])) { index = index * 10 + atoi(line[k]); k++; } printf("%s", word[nwords - index]); strcpy(tword, word[nwords-index]); for(j = index + 1; j < nwords; j++) { strcpy(word[j - 1], word[j]); } strcpy(word[nwords - 1], tword); } else { printf("%c", line[k]); k++; } } }
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
And why do you keep a separate variable
Your number extraction is totally screwed. You only feed a single char to
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!
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."
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 2
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.
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.
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.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int find_word(const char ** array, const int size, const char * item) { int i = 0; while (i < size) { if (strcmp(array[i], item) == 0) return i; else i++; } return -1; } int main(int argc, char *argv[]) { char filename[65536], outputfilename[65536]; FILE * file; FILE * outputfile; char buffer[256]; char * array[4096]; int size = 0; int pos; if (argc == 1) { printf("Not enought parameters!\n"); return 1; } if (argc > 1) { strcpy(filename, argv[1]); } if (argc > 2) { strcpy(outputfilename, argv[2]); } else { strcpy(outputfilename, filename); strcat(outputfilename, ".new"); } file = fopen(filename, "r"); outputfile = fopen(outputfilename, "w"); if ((file == NULL) || (outputfile == NULL)) { printf("Error opening file\n"); return 2; } while(!feof(file)) { if (fgets(buffer, 256, file) == NULL) break; pos = strlen(buffer) - 1; if (buffer[pos] == '\n') buffer[pos] = 0; // remove trailing LFCR's if (isalpha(buffer[0])) // word { pos = find_word(array, size, buffer); if (pos == -1) // word is not in the array, must add it. { if (size == 4095) exit(3); // size check for array array[size] = (char *) malloc(256 * sizeof(char)); if (array[size] == NULL) exit(3); // check malloc()!! strcpy(array[size], buffer); printf("%s added at %i\n", buffer, size); size++; } // nothing else, do nothing on duplicate words } // end of if -- isalpha else // word is already in the array! { pos = atoi(buffer); if (pos >= size) { printf("Error! %d not in the array!!\n", pos); } else { strcpy(buffer, array[pos]); printf("Replacing %d with %s\n", pos, buffer); } } fputs(buffer, outputfile); fprintf(outputfile,"\n"); } fclose(file); fclose(outputfile); }
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: pascal triangle?
- Next Thread: undeclared identifier error
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






