C-Programming (Both question Q1 and Q2 are linked. do them seperately) Q1. Read
ID: 3718845 • Letter: C
Question
C-Programming (Both question Q1 and Q2 are linked. do them seperately)
Q1. Read from console input two strings and check if the second string (length < 20) is a substring of the first one (length < 100). You cannot assume the length of the input strings. The first input string may contain spaces. Note, You cannot call functions in string.h
Example if you;
Input: “abcd efghijk12 34567” “hij”
Ouput: YES
Q2. Concatenate the two input strings and store the result in a char array. You cannot assume the length of the input strings. The first input string may contain spaces.
Note: You cannot call functions in string.h
E.g.: input “abcd efghijk12 34567” “hij” , You should first store “abcd efghijk12 34567hij” in an array and then print it.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
//method to check c is substring of a
int is_substring(char a[],char c[])
{
int n=0,m=0;
while(a[n]!='')n++;//finding length of a
while(c[m]!='')m++;//finding length of c
int i=0,j;
if(m<=n)
{while(i<=(n-m))
{
j=0;
while(j<n)
{
if(a[i+j]!=c[j])break;
j++;
}
if(j==m)return 1;//if substring..
i++;
}
}
return 0;//if not substring...
}
int main()
{
char c[500],a[500];//declaraing char array to store string ...
printf("Enter stirng1:");
scanf("%1023[^ ]",a);
char d=getchar();
printf("Enter stirng2:");
scanf("%1023[^ ]", c);
if(is_substring(a,c)==1)printf("YES ");
else printf("NO ");
return 0;
}
output:
Enter stirng1:abcd bca
Enter stirng2:db
NO
Process exited normally.
Press any key to continue . . .
Enter stirng1:abcd efghijk12 34567
Enter stirng2:hij
YES
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.