Help on code that compares strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Will2k
    New Member
    • Feb 2007
    • 10

    Help on code that compares strings

    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.

    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;
    }
    ANY suggestions?
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    You are printing out the string pointed to by indxptr. The problem is that indxptr has never been initialized, and is pointing to who knows what. That's the garbage you're printing out. Your program could also crash and burn with some indxptr values.

    It looks like you want indxptr to hold the value returned by strmatch, but never call strmatch. Don't be fooled with the definition of indxptr inside strmatch. That indxptr is different from the indxptr in main. Once again, if you want indxptr in main to contain a value, you have to set it to one.

    With pointers, its best if you initialize them with 0 or some real pointer value. If you had, then your logic in main would have caught the problem.

    Comment

    Working...