String Assignment
String Assignment
Roll:9
Topic:String(Assignment)
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)
else
printf("Not palindrome");
}
Take 1 string as input. Count the number
of vowels, consonants,
#include <stdio.h>
#include <ctype.h>
int main() {
char str[1000];
lowercase = 0, other = 0;
gets(str);
if (isalpha(str[i]))
char ch = tolower(str[i]);
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++;
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 n 25
void main(){
char str[100];
int ch_fre[n];
int i=0,max;
int ascii;
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;
}
Write a program in C to extract a
substring from a given string
#include <stdio.h>
#include <string.h>
int main() {
gets(str);
scanf("%d",&start);
scanf("%d",&length);
j = 0;
substr[j]=str[i];
j++;
substr[j] = '\0';
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() {
gets(str);
gets(substring);
} else {
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;
gets(str);
for(i=0; str[i]!='\0';i++)
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);
scanf("%c",&ch);
ch=tolower(ch);
for(i=0;str[i]!='\0';i++)
if(ch==str[i])
count++;
getch();
return 0;