Home>
Problem i am experiencing

To search for a specific character string in the character string entered from the keyboard
I wrote the following code, but
The result is not output.
How should I fix it?

Condition

N represents the number of characters to be entered.
S is a specific character string T is the character string to be entered.
The strings must be in order.
It is valid if the input string contains only one character.
Example) Check string "take"
The entered string "mistake" valid
"mistook" invalid
"mitaike" valid

# include<stdio.h>
int main (void) {
    int N, i, j, k;
    char S [11];
    char T [21];
    scanf ("% d",&N);
    scanf ("% s", S);
    for (i = 0;i<N;i ++) {
        scanf ("% s", T);
        j = 0;
        k = 0;
        while (S [j]! = '\ 0') {
            while (T [k]! = '\ 0') {
                if (T [k] == S [j]) {
                   while (T [k]! = S [j]) {
                        k ++;
                        j ++;
                        if (T [k]! = S [j]) {
                            k ++;
                            if (T [k]! = S [j]) {
                                k--;
                                k--;
                                j--;
                                break;
                            }
                        }
                        if (S [j + 1] == '\ 0') {
                            printf ("valid \ n");
                            break;
                        }
                   }
                }
                k ++;
            }
            j ++;
        }
        printf ("invalid \ n");
    }
    return 0;
}
c
  • Answer # 1

    Created and tested with Compile and Execute C Online (GNU GCC v7.1.1).
    The input section is broken.

    # include
    #include
    / * Maximum number of characters * /
    #define SUPERFLUOUS_LETTER_MAX 1
    #define VALID 1
    #define INVALID 0
    int find (char * s, char * t) {
      int i, ii, j, c;
      for (i = 0;i<= (int) strlen (t)-(int) strlen (s);i ++) {
        if (t [i] == s [0]) {
          c = 0;/* Character counter * /
          for (j = 0, ii = i;s [j]! = '\ 0';ii ++) {
            if (t [ii] == s [j]) {
              j ++;
            } else if (++ c>SUPERFLUOUS_LETTER_MAX) {
              break;
            }
          }
          if (c<= SUPERFLUOUS_LETTER_MAX) return VALID;
        }
      }
      return INVALID;
    }
    void printFind (char * s, char * t) {
      printf ("S = '% s', T = '% s':% s \ n",
          s, t, find (s, t)? "valid": "invalid");
    }
    int main () {
      char * s = "take";
      printFind (s, "");
      printFind (s, "tak");
      printFind (s, "take");
      printFind (s, "takie");
      printFind (s, "taakie");
      printf ("\ n");
      printFind (s, "mistake");
      printFind (s, "mistook");
      printFind (s, "mitaike");
      return 0;
    }
    S = 'take', T = '': invalid
    S = 'take', T = 'tak': invalid
    S = 'take', T = 'take': valid
    S = 'take', T = 'takie': valid
    S = 'take', T = 'taakie': invalid
    S = 'take', T = 'mistake': valid
    S = 'take', T = 'mistook': invalid
    S = 'take', T = 'mitaike': valid

  • Answer # 2

    I tried using strstr and strncmp.

    # include// scanf, puts
    #include// strlen, strstr, strncmp
    int main (void)
    {
        int N, i, j, valid, len;
        char S [110], T [210], U [110], * p;
        if (scanf ("% d",&N)! = 1) return 1;
        scanf ("% 109s", S);
        for (i = 0;i

Related articles