Well, actually I'm trying to create a program that receives strings from a file and replaces a few words from some kind of a source (another file) and swap between words that are in the source of the words.
These words are shown this way in the file:
and in the original file there are strings, such as:Code:elephant fat mouse thin smart stupid
So now, I'm trying to receive the data from the strings-file and swap words that are shown in the source file and create a new sentence by swapping these words.Code:This guy is really smart!
e.g:
original file:
After compiling:Code:You are smart
Here's what I've got so far:Code:You are stupid.
Code:#include <stdio.h> #include <string.h> #define LENGTH 50 char *newSubstr(char *str, int index); char **explode(char *str, char seperator); int main(void) { FILE *handle, *db; char *filename, str[LENGTH]; printf("File: "); fgets(filename, 16, stdin); handle = fopen(filename, "rt"); db = fopen("database.txt", "rt"); while(fgets(str, LENGTH, handle) != NULL) { for(int i = 0; i < strlen(str); i++) { if(str[i] == ' ' && i + 1 < strlen(str)) { char search[LENGTH], **results; while(fgets(search, LENGTH, db) != NULL) { results = explode(search, ' '); if(strcmp(results[0], newSubstr(str, i+1)) == 0) // if equals, swap the string with results[1] printf("Match! %s - %s", results[0], results[1]); } } } } getchar(); return 0; } char *newSubstr(char *str, int index) { int i = index, j= 0; char *newstr = ""; for(; i < strlen(str); i++) { if(str[i] == ' ') break; newstr[j++] = str[i]; } return newstr; } char **explode(char *str, char seperator) // explodes ONLY 2 strings that are seperated by a character { int i = 0, index = 0; for(; i < strlen(str); i++) if(str[i] == seperator) break; char **newexplode; if(i != 0 && i != strlen(str) - 1) { int index_0 = 0, index_1 = 0; int j = 0; for(; j < i; j++) newexplode[0][index_0++] = str[j]; for(; i < strlen(str); i++) newexplode[1][index_1++] = str[i]; } return newexplode; }
I'm pretty sure (actually, I know) that there is a problem with the return values of `explode` and `newSubstr`...I dunno, look at this code for example:
Really basic but still, `str` contains total garbage, and I don't know why...Code:char *newSubstr(char *str, int index) { int i = index, j = 0; char newstr[20]; for(; i < strlen(str); i++) { if(str[i] == ' ') break; newstr[j++] = str[i]; } return newstr; } int main(void) { char *str = newSubstr("Hello world", 6); printf("%s", str); getchar(); return 0; }
It's pretty basic what I've done here so please help me ASAP (these are just pointers and return values, come on)



LinkBack URL
About LinkBacks
) 



