Problem solving questions on strings | C Language
Strings is the important part of C language . Strings also known as char array.
- Char array is used to store strings
- A string is the sequence of char
- Whenever a string is stored in char array its end is marked with a special NULL char i.e. '\0'
- Char array can be created by using char datatype
- Syntax: char arrayname[size];
- for ex: char a[10];
- char a[5]={'f','s','a','\0'};
- char a[]={'f','s','a','\0'};
- char a[5]="ram";
- char a[]="ram";
1*// wap to display a string
#include<stdio.h>
main()
{
char a[20];
printf("Enter the string\n");
scanf("%s",a);
printf("Hello %s",a);
}
Output:
Enter the string
Tilak
Hello Tilak
2*#include<stdio.h>
main()
{
char a[20];
printf("Enter the string\n");
gets(a);
printf("Hello ");
puts(a);
}
Output:
Enter the string
Tilak Bhusare
Hello Tilak Bhusare
3*// WAP to read a string and find length without using library function.
#include<stdio.h>
main()
{
char a[50];
int i;
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
i++;
}
printf("Length is %d",i);
}
Output:
Enter the string
Tilak Sanjay Bhusare
Length is 20 // The compiler itself count the spaces along with char { char 18 spaces 2 total 20}
4*// WAP to read string and count total no of char 'a' in it.
#include<stdio.h>
main()
{
char a[50];
int i,c=0;
printf("Enter the string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a')
c=c+1;
}
printf("Total no 'a' is %d",c);
}
Output:
Enter the string
India
Total no 'a' is 1
5*// WAP to read string and count total no of vowels in it.
#include<stdio.h>
main()
{
char a[50];
int i,c=0;
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')
c=c+1;
i++;
}
printf("total no of vowels are : %d",c);
}
Output:
Enter the string
Amravati
total no of vowels are : 4
6*// WAP to read string and count total no of words in it.
#include<stdio.h>
main()
{
char a[100];
int i,c=0;
printf("Enter the string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
c=c+1;
}
printf("Total no of words are : %d",c);
}
Output:
Enter the string
I Love Programming
Total no of words are : 3
7*// Wap to read string and convert it into upper case.
#include<stdio.h>
main()
{
char a[50];
int i;
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
if(a[i]>='a' && a[i]<='z')
a[i]=a[i]-32;
i++;
}
printf("Result is %s",a);
}
Output:
Enter the string
amravati
Result is AMRAVATI
Comments