Assignment-3: 1. Write A Program in C To Extract A Substring From A Given String
Assignment-3: 1. Write A Program in C To Extract A Substring From A Given String
#include <stdio.h>
#include <string.h>
return 1;
return 1;
subString[j] = string[i];
return 0;
int main()
char string[100];
char subString[50];
int from,to;
fgets(string,100,stdin);
scanf("%d",&from);
scanf("%d",&to);
else
printf("ERROR!\n");
return 0;
OUTPUT:
Enter a string: Welcome to StackHowTo
2.Write a C program to check whether a given substring is present in the given string
#include <stdio.h>
void main()
{
char str[80],search[20];
int c1=0,c2=0,i,j,flg;
while (str[c1]!='\0')
c1++;
c1--;
while (search[c2]!='\0')
c2++;
c2--;
for(i=0;i<=c1-c2;i++)
{
for(j=i;j<i+c2;j++)
{
flg=1;
if (str[j]!=search[j-i])
{
flg=0;
break;
}
}
if (flg==1)
break;
}
if (flg==1)
printf("The substring exists in the string.\n\n");
else
printf("The substring is not exists in the string. \n\n");
}
OUTPUT:
Input the string : This is a test string.
Input the substring to be search : search
The substring is not exists in the string.
3.Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100];
int ctr, ch, i;
i=strlen(str);
OUTPUT:
Input the string : This Is A Test String
4. Write a program in C to find the number of times a given word 'the' appears in the given string.
#include <stdio.h>
#include <string.h>
void main()
{
int ctr=0,i,freq=0;
int t,h,e,spc;
char str[100];
printf("\n\nFind the number of times the word 'the ' in any combination appears :\n");
printf("----------------------------------------------------------------------\n");
ctr=strlen(str);