r/cprogramming • • Jun 21 '23

Q. To check whether a given string has any match with main string. Can you explain why the code below doesn't work and how to fix it?

include<stdio.h>

include<string.h>

int main() { char s[20],t[20]; int i,j,l1,l2; fgets(s,20,stdin); fgets(t,20,stdin); l1=strlen(s); l2=strlen(t); for(i=0;i<l1;i++) {j=0; if(s[i]==t[j]) { i++; j++;

} printf("%i",j); if(j==l2) { printf("match found at pos %i",i-l2+1); } } }

0 Upvotes

9 comments sorted by

3

u/Willsxyz Jun 21 '23
include<stdio.h>
include<string.h>
int main()
{ 
    char s[20],t[20];
    int i,j,l1,l2;

    fgets(s,20,stdin);
    fgets(t,20,stdin);
    l1=strlen(s);
    l2=strlen(t);

    for(i=0;i<l1;i++) {
        j=0;
        if(s[i]==t[j]) {
            i++;
            j++;
        }

        printf("%i",j);
        if(j==l2) {
            printf("match found at pos %i",i-l2+1);
        }
    }
}

1

u/Bitwise_Gamgee Jun 21 '23
  1. You should remove the new line character from stdin:

    fgets(s, sizeof(s), stdin); s[strcspn(s, "\n")] = '\0';

  2. if (s[i] == t[j]) should not result in increments, incrementing i here will mess up your for loop.

  3. printf("match found at pos %i",i-l2+1); } needs to be within a conditional

1

u/IamImposter Jun 21 '23

Format the code please. It's hard to follow

To find what's wrong with any code, you can do a few things yourself

  • run your code using a debugger, add breakpoints, singlestep through the code in question and examine the values in different variables as and when they change. Debugger is a useful skill and you will have to learn it at some point. Why not right now!

  • add print statements to your code, as many as you can and print values of relevant variables. Throw in a few scanfs so the code stops executing every few statements and you can examine the values. Too much text can make it hard to spot the issue.

So I'm assuming first string is given string and second is main

for(i=0;i<l1;i++) {
  j=0; 
  if(s[i]==t[j]) { 
    i++; j++;
  } 
  printf("%i",j); 
  if(j==l2) { 
    printf("match found at pos %i",i-l2+1); 
  } 
}

It looks like first is main and second is given. Let's take an example and see what happens. So main is 'my textile' and given is 'text'

  • for loop starts

  • we pick first letter, 'm' and 't'. They don't match. So not increment

  • j is printed as 0

  • j is not equal to l2. So not print statement

Next iteration - i=1, j=0:

  • we pick 'y' and 't'. No match

Next i=2, j=0:

  • we pick ' ' and 't'. No match. Same

Next i=3, j=0:

  • we pick 't' and 't'. Match

  • increment, i=4, j=1. j not equal to l2

Next i=5, j=0; <--- see that j=0

  • we pick 'x' and 't' again

Shouldn't we have picked 'e' and 'e'? See what happened? We double incremented i and wrongly reset j

Guess an else is needed and reset j only if there is a mismatch. And don't increment i on match.

1

u/petecasso0619 Jun 21 '23

You keep resetting j in the for loop. How will j ever be anything other than 0 or 1? Seems like you should be using l2 and have another nested loop.

1

u/saul_soprano Jun 21 '23 edited Jun 21 '23

Your loop sets j to 0 then compares it to s[i], and only if those match is j incremented? And i too? Then if j is equal to the length of t it prints a match is found? I don’t really know how that was supposed to work. Just use a nested for loop

for (i = 0; i < l1; i++) { for (j = 0; j < l2; j++) { if (s[i] == t[j]) printf(“Match found at %d, %d\n”, i, j); } }

1

u/Poddster Jun 21 '23

Well for starters, %d is used to print ints, not %i

%i is correct

Conversion specifiers

A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are:

d, i The int argument is converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty.

2

u/saul_soprano Jun 21 '23

Well damn now I feel like an idiot. Lemme just remove that real quick…

2

u/Poddster Jun 21 '23

%d and %u were the OG way. %i was added for ANSI C, and along with it modifiers like %ui. So your knowledge is only 35 years out of date ;)