I am writting that code that takes in two strings and compares them, then outputs a new string.
It goes like this
Hello their. How are you?
Hel
lo their. How are you?
Now I have a replace function, and string match function and a driver that takes in two strings. Everytime I take in two strings, it outputs some weird stuff. I have no idea what is up with it.
ANY suggestions?
It goes like this
Hello their. How are you?
Hel
lo their. How are you?
Now I have a replace function, and string match function and a driver that takes in two strings. Everytime I take in two strings, it outputs some weird stuff. I have no idea what is up with it.
Code:
#include <stdio.h> int sreplace(char newc, char oldc, char *s) { int i; int cnt; int len = strlen(s); i = cnt = 0; while(i<len) //loop to run till the end of the string { if(s[i] == oldc) //if the char found which to be replaced { s[i] = newc; //replace cnt++; } i++; } return cnt; }
Code:
#include <stdio.h> char *strmatch(char *str, char *s) { int len1; int len2; char *indxptr; int i; int j; int k; int cnt; i = j = k = cnt =0; len1 = strlen(str); //length of string 1 len2 = strlen(s); //length of string 2 while(i<len1) { if(str[i] == s[0]) { k = i; //saves state of i while(j<len2) //loop to check till end of string 2 { if(str[i] == s[j]) //if still equal { i++; indxptr = &str[i]; cnt++; if(cnt == len2) return indxptr; } else { indxptr = NULL; j = 0; i = k; cnt = 0; break; } j++; } } i++; } return indxptr; }
Code:
#include <stdio.h> #include "strmatch.c" #include "sreplace.c" int main() { char *line1; char *line2; int c; line1 = (char *) malloc(1024); line2 = (char *) malloc(1024); char *indxptr; int rep1; int rep2; while((c = getchar()) != EOF) { fgets(line1,1024,stdin); fgets(line2,1024,stdin); rep1 = sreplace('\0','\n', line1); rep2 = sreplace('\0','\n', line2); if(indxptr == NULL) //if not match return -1; else printf("%s\n",indxptr);//if found } getchar(); return 0; }
Comment