c - I can't read the second string, no matter how many getchar i insert -
this program need read 2 strings, 2 strings passed "confirm" function, read , the function have find word in common.
but in main cant read "string2" string! no matter how many getchar insert.
#include <stdio.h> #include <string.h> void confirm(char string1[],char string2[]){ char word[20]; char w_aux[20]; int i, j, k, l, m; int len1, len2; int find; = 0; len1 = strlen(string1); len2 = strlen(string2); while (i < len1){ j = 0; while ((string1[i] != ' ') && (i < len1)){ word[j] = string1[i]; i++; j++; } word[j] = '\0'; k = 0; find = 0; while((k < len2) && (find == 0)){ l = 0; while ((string2[k] != ' ') && (k < len2)){ w_aux[l] = string2[k]; k++; l++; } w_aux[l] = '\0'; if (j == l){ m = 0; while((word[m] == w_aux[m]) && (m<j)){ m++; } if (m == j){ find = 1; } } k++; } i++; } printf("%s\n", word); } int main (){ char string1[20]; char string2[20]; printf("type first text: \n"); scanf("%[^\n]s", string1); printf("type second text: \n"); scanf("%[^\n]s", string2); getchar(); confirm(string1, string2); return 0; }
use getchar
after input of string 1 \n
:
printf("type first text: \n"); scanf("%[^\n]", string1); getchar(); //here printf("type second text: \n"); scanf("%[^\n]", string2);
Comments
Post a Comment