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

String Assignment

Uploaded by

roysid319
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)
11 views

String Assignment

Uploaded by

roysid319
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/ 18

Moshrur Ahmad Siam

Roll:9
Topic:String(Assignment)

Take 2 string as input. Swap them.


#include<stdio.h>

int main(){

char s1[200]="Bangladesh";

char s2[200]="India";

char temp[30];

printf("Before swapping\n");

printf("s1=%s\n",s1);

printf("s2=%s\n",s2);

strcpy(temp,s1);

strcpy(s1,s2);

strcpy(s2,temp);

printf("\n\nAfter swapping\n");

printf("s1=%s\n",s1);

printf("s2=%s\n",s2);

return 0;

}
Take 1 string as input. Check
whether it is palindrome or not.
#include<stdio.h>

#include<string.h>

int main()

char s1[100],s2[100];

printf("Enter a string:");

gets(s1);

strcpy(s2,s1);

strrev(s2);

if(strcmp(s1,s2)==0)

printf("The string is a palindrome");

else

printf("Not palindrome");

}
Take 1 string as input. Count the number
of vowels, consonants,

Digits, words, uppercase letters, lowercase


letters and other characters.

#include <stdio.h>

#include <ctype.h>

int main() {

char str[1000];

int i, vowels = 0, consonants = 0, digits = 0, words = 0, uppercase = 0,

lowercase = 0, other = 0;

printf("Enter a string: ");

gets(str);

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

if (isalpha(str[i]))

char ch = tolower(str[i]);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

vowels++;

else

consonants++;

}
if (isupper(str[i]))

uppercase++;

else

lowercase++;

} else if (isdigit(str[i]))

digits++;

} else if (isspace(str[i]))

words++;

else

other++;

if (i > 0)

words++;

printf("\nNo.of vowels: %d\n",vowels);

printf("No.of consonants: %d\n",consonants);

printf("No.of digits: %d\n",digits);

printf("No.of words: %d\n",words);


printf("No.of uppercase letters: %d\n",uppercase);

printf("No.of lowercase letters: %d\n",lowercase);

printf("No.of others: %d\n",other);

return 0;

}
Write a program in C to find the
maximum number of characters in a
string.
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define str_size 100

#define n 25

void main(){

char str[100];

int ch_fre[n];

int i=0,max;

int ascii;

printf("Input the string:");

fgets(str,sizeof str,stdin);

for (i=0;i<n;i++)

{
ch_fre[i]=0;

i=0;

while (str[i]!='\0'){

ascii=(int)str[i];

ch_fre[ascii]+=1;

i++;

max=0;

for(i=0;i<n;i++){

if(i!=32){

if(ch_fre[i]>ch_fre[max])

max=i;

printf("The most frequent character :%c\nAppears number of time:%d\n\n",max,ch_fre[max]);

}
Write a program in C to extract a
substring from a given string
#include <stdio.h>

#include <string.h>

int main() {

char str[1000], substr[1000];

int start, length, i, j;

printf("Input the string:");

gets(str);

printf("Input the position to start extraction:");

scanf("%d",&start);

printf("Input the length of substring:");

scanf("%d",&length);

j = 0;

for (i=start-1; i<start+length && str[i] != '\0';i++)

substr[j]=str[i];

j++;

substr[j] = '\0';

printf("The substring retrieve from the string is:%s\n",substr);

getch();

return 0;

}
Write a C program to check whether
a substring is present in a string.
#include <stdio.h>

#include <string.h>

int main() {

char str[1000], substring[1000];

printf("Input the string:");

gets(str);

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

gets(substring);

if (strstr(str, substring) != NULL) {

printf("\n\nThe substring exists in the string.\n");

} else {

printf("The substring is not exists in the string.\n");

return 0;

}
Write a program in C to remove
characters from a string except
alphabets.
#include <stdio.h>

int main(){

char str[150];

int i,j;

printf("Input the string:");

gets(str);

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

while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))

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

str[j]=str[j+1];

str[j]='\0';

printf("\n\nAfter removal:%s",str);

getch();

return 0;

}
Write a program in C to find the
frequency of characters
#include <stdio.h>

void main(){

char str[500],ch;

int i,count=0;

printf("Input string:");

gets(str);

printf("Enter the character to count frequency:");

scanf("%c",&ch);

ch=tolower(ch);

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

if(ch==str[i])

count++;

printf("The frequency of '%c' is:%d\n\n",ch,count);

getch();

return 0;

You might also like