You are on page 1of 4

(a) Length of the string

#include <stdio.h>

void main()
{
char string[50];
int i, length = 0;

printf("Enter a string \n");


scanf("%s", string);

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


{
length++;
}

printf("So, the length string %s = %d\n", string, length);


}

Enter a string
mohan
So, the length string mohan = 5

-------------------------------------------------

(b) Total number of characters in the string

#include <stdio.h>

void main()
{
char string[50];
int i, length = 0;

printf("Enter a string \n");


scanf("%s", string);

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


{
length++;
}

printf("So, the total no. of character in string %s = %d\n", string, length);


}
Enter a string
mohan
So, the total no. of character in string mohan = 5

-------------------------------------------------

(c) Total number of vowels in the string

#include <stdio.h>

int main()
{
char line[150];
int i, vowels;

vowels = 0;

printf("Enter a line of string: ");


scanf("%[^\n]", line);

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


{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
}

printf("Vowels: %d",vowels);
return 0;
}

Enter a line of string: mohan


Vowels: 2

--------------------------------------------------
(d) Reverse of the string.

#include<stdio.h>

int main()
{
char s[1000], r[1000];
int begin, end, count = 0;

printf("Input a string\n");
scanf("%s", s);

while (s[count] != '\0')


count++;

end = count - 1;

for (begin = 0; begin < count; begin++) {


r[begin] = s[end];
end--;
}

r[begin] = '\0';

printf("%s\n", r);

return 0;
}

Input a string
bibahk
khabib

----------------------------------------------------

(e) Whether the string is a palindrome.


#include<stdio.h>

int main(){
int i,pal=0,len=0;
char str[100];
printf("Enter a string to check whether it is palindrome or not: ");
scanf("%s", str);
for(i=0;i<100;i++){
if(str[i]=='\0'){
break;
}
len++;
}
for(i=0;i<len;i++){
if(str[i]==str[len-1-i]){
pal++;
}
}
if(len==pal){
printf("Entered string is palindrome\n");
}
else{
printf("Entered string is not palindrome\n");
}
return 0;
}

Enter a string to check whether it is palindrome or not: nitin


Entered string is palindrome

---------------------------------------------------

You might also like