Advertisement
userxbw

Search pattern in 2d char arrays in C

Aug 13th, 2022
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int main(){
  5.  
  6. char a[]={'a','b','c'};
  7. char *b[]={
  8.  
  9. /*0*/"hello",
  10. /*1*/"abby",
  11. /*2*/"cheeze",
  12. /*3*/"ball",
  13. /*4*/"cats"
  14.       };
  15. /* frist figure out how to get
  16.    length of 2d array char*/
  17. int sc=0;
  18. for (int i=0;i<5;i++){
  19.       sc=strlen(b[i]);
  20.     printf("strlen *b[%d]=%lu\n",
  21.      i,strlen(b[i]));
  22.     }
  23. printf("\n");
  24. /* then figure out how to
  25.   iterare the each word */
  26.  for(int i=0;i<5;i++){
  27.     sc=strlen(b[i]);
  28.     for(int k=0;k<sc;k++){
  29.        printf("%c ",b[i][k]);
  30.     }
  31.   printf("\n");
  32.  }
  33. /* now figure out how to use
  34.    a search string of a single
  35.    char to find the matching
  36.    letter in each word */
  37. /* get length of each word
  38.    2d char array*/
  39.  for(int i=0;i<5;i++)
  40.  {
  41.     sc=strlen(b[i]);
  42.    /* search pattern */
  43.    for(int e=0;e<3;e++)
  44.    {/* iterate each letter of
  45.        word */
  46.      for(int k=0;k<sc;k++)
  47.      { /*check search pattern
  48.          againt each each char
  49.          in string */
  50.           if(a[e]==b[i][k])
  51.           {
  52.              printf("\nfound %c=%c\n"
  53.              "word is: %s\n",
  54.               a[e],b[i][k],b[i]);
  55.           }
  56.       }
  57.     }
  58.   printf("\n");
  59.  }
  60.  
  61. return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement