0% found this document useful (0 votes)
31 views

Assignment-3: 1. Write A Program in C To Extract A Substring From A Given String

Uploaded by

aryan86ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment-3: 1. Write A Program in C To Extract A Substring From A Given String

Uploaded by

aryan86ya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT-3

1. Write a program in C to extract a substring from a given string.

#include <stdio.h>

#include <string.h>

int extract(int from, int to, char *string, char *subString)

int i=0, j=0;

//get the length of the string.

int length = strlen(string);

if( from > length || from < 0 ){

printf("The index 'from' is invalid\n");

return 1;

if( to > length ){

printf("The index 'to' is invalid\n");

return 1;

for( i = from, j = 0; i <= to; i++, j++){

subString[j] = string[i];

return 0;
int main()

char string[100];

char subString[50];

int from,to;

printf("Enter a string: ");

fgets(string,100,stdin);

printf("Enter the index 'from': ");

scanf("%d",&from);

printf("Enter the index 'to': ");

scanf("%d",&to);

printf("The string is : %s",string);

if( extract(from, to, string, subString) == 0 )

printf("The sub-string is : %s", subString);

else

printf("ERROR!\n");

return 0;

OUTPUT:
Enter a string: Welcome to StackHowTo

Enter the index 'from': 11

Enter the index 'to': 20

The string is : Welcome to StackHowTo

The sub-string is : 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;

printf("\n\nCheck whether a given substring is present in the given string


:\n");
printf("-------------------------------------------------------------------\n");

printf("Input the string : ");


fgets(str, sizeof str, stdin);

printf("Input the substring to be search : ");


fgets(search, sizeof search, stdin);

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;

printf("\n\nReplace lowercase characters by uppercase and vice-versa :\n");


printf("--------------------------------------------------------------\n");

printf("Input the string : ");


fgets(str, sizeof str, stdin);

i=strlen(str);

ctr = i; /*shows the number of chars accepted in a sentence*/

printf("\nThe given sentence is : %s",str);

printf("After Case changed the string is: ");


for(i=0; i < ctr; i++)
{
ch = islower(str[i]) ? toupper(str[i]) : tolower(str[i]);
putchar(ch);
}
printf("\n\n");

OUTPUT:
Input the string : This Is A Test String

The given sentence is : This Is A Test String


After Case changed the string is: 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");

printf("Input the string : ");


fgets(str,sizeof str,stdin);

ctr=strlen(str);

/*Counts the frequency of the word 'the' with a trailing space*/


for(i=0;i<=ctr-3;i++)
{
t=(str[i]=='t'||str[i]=='T');
h=(str[i+1]=='h'||str[i+1]=='H');
e=(str[i+2]=='e'||str[i+2]=='E');
spc=(str[i+3]==' '||str[i+3]=='\0');
if ((t&&h&&e&&spc)==1)
freq++;
}
printf("The frequency of the word \'the\' is : %d\n\n",freq);
}
OUTPUT:
Input the string : The stering where the word the present more then onces.
The frequency of the word 'the' is : 3
5. Write a program in C to Find the Frequency of Characters
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");


scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i) {


if (ch == str[i])
++count;
}

printf("Frequency of %c = %d", ch, count);


return 0;
}
Output
Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4

You might also like