0

So the task at hand is that I need to make a code to input firstly the minimal size of two arrays. Then the condition for array A is that it should only input lowercase letters and for array B just letters, uppercase and lowercase included. Afterwards I need to scan both arrays for triplets of inserted letters and then see whether the subsets of 3 are equal for both arrays and then make a substring for it. While scanning uppercase and lowercase letters should be treated as same so I wanted to use strcmpi but I was not able to so I used strcasecmp.

For example I have arrayA=(a s d r e d b l u) and arrayB=( d B l u a s d R e d) so the substring would look like this substring=(red blu) as red and blu are both the same triplets for both arrays

I cannot use break or go to or switch, that is what our teacher said, and we also cannot input this array as a string as we need to input it as char type. If anyone can give any input I would be glad as I do not know how to proceed. I assumed I should maybe use gets because, even though I do not have syntax errors, it does not exit the input after I press space or enter.

#include <stdio.h>
#include <string.h>

int main()
{
    int i=0,j=0,s,m,n;
    char nizA[100], nizB[100], podstring[100], pom, niz1p[100], niz2p[100];
    printf("Enter the minimal value of array A\n");
    scanf("%d",&m);
    printf("Enter the minimal value of array B\n");
    scanf("%d",&n);
    do{
        printf("Enter elements of array A\n");
        for(i=0;nizA[i]<m;i++)
        {
            scanf("%c",&nizA[i]);
            if (nizA[i]<'a' && nizA[i]>'z')
            {
                i--;
                printf("The required condition is not met.");
            }
        }
        
        
    }while(nizA[i]<'a' && nizA[i]>'z');
 do {
     
    printf("Insert elements of the array B:\n");
        for(j=0;nizB[j]<m;j++)
         {
            scanf("%c",&pom);
            if (nizA[i]<'a' && nizA[i]>'z' || nizA[i]<'A' && nizA[i]>'Z')
            {
                nizB[j]=pom;
            }
                else
                {
                    j--;
                 
               }
        }

    }while(nizA[i]<'a' && nizA[i]>'z' || nizA[i]<'A' && nizA[i]>'Z');
 printf("Entered array  A is:\n");
 printf("%c",nizA[i]);
 printf("Entered array B is\n");
 printf("%c",nizB[j]);
  for(i=0;nizA[j]<m;i++)
  {
   for(j=0;nizB[j]<m;j++)
   {
       niz1p[0]=nizA[i];
       niz1p[1]=nizA[i+1];
       niz1p[2]=nizA[i+2];
       niz2p[0]=nizA[i];
       niz2p[1]=nizA[i+1];
       niz2p[2]=nizA[i+2];
       if (strcasecmp(niz1p,niz2p))
       {
           podstring[s+1]=nizA[i];
           podstring[s+2]=nizA[i+1];
           podstring[s+3]=nizA[i+2];
       }
   }
      
  }

printf("The substring is\n");
printf("%c",podstring[s]);

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please be aware that [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. So you might want to use `scanf(" %c",&nizA[i]);` with the added space shown, although it isn't clear whether the input contains spaces to be kept. If so, they would be removed. – Weather Vane Jul 05 '22 at 22:23
  • @WeatherVane What do you mean by it is always true? Also how should I even input characters into an array, should I press enter after I insert them or just space? – asdasdasd asdasdasd Jul 05 '22 at 22:36
  • I deleted that, just exploring it. As for the first comment, you can separate by newlines or not as you please. You haven't made it clear whether or not spaces are required in the input. Either way, a newline is left in the buffer after the previous use of `%d`. And `%c` reads *every* character unless you tell it not to. – Weather Vane Jul 05 '22 at 22:37
  • 1
    BTW `if (nizA[i]<'a' && nizA[i]>'z' || nizA[i]<'A' && nizA[i]>'Z')` is always false. Did you mean `if (nizA[i]>='a' && nizA[i]<='z' || nizA[i]>='A' && nizA[i]<='Z')`? Or even better `if(isalpha(nizA[i]))` with `#include `. – Weather Vane Jul 05 '22 at 22:44
  • @WeatherVane So in the output the teacher sent us there are spaces between every character in the array if that helps with anything. Also we cannot use any other header files such as ctype.h. I meant your second thing it is probably my mistake on that. This is what I meant if (nizA[i]>='a' && nizA[i]<='z' || nizA[i]>='A' && nizA[i]<='Z') – asdasdasd asdasdasd Jul 05 '22 at 22:50
  • 3
    @asdasdasdasdasdasd "assumed I should maybe use gets" --> No, never use `gets()`. `gets()` has been removed from C for over 10 years. If your training encourages that, consider the training out-of-date. – chux - Reinstate Monica Jul 06 '22 at 00:02
  • `for(i=0;nizA[i] `for(i=0;i – Craig Estey Jul 06 '22 at 00:16

0 Answers0