Assignment-Ix: Q1. Write A Program To Find The Length of A String Without Using Strlen Function. Answer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ASSIGNMENT-IX

Q1. Write a program to find the length of a string without using strlen
function.
Answer:
#include<stdio.h>
int main()
{
int size,count=0;
printf("Enter the size of the array ");
scanf("%d",&size);
char string[size];
printf("Enter the string ");
scanf("%s",string);
for(int i=0;string[i]!='\0';i++)
{
count++;
}
printf("The length of the string is %d\n",count);

return 0;
}

Q2. Write a program to count number of vowels in a string.


Answer:
#include<stdio.h>
int main()
{
int count=0,size;
printf("Enter the size of string ");
scanf("%d",&size);

char string[size];
printf("Enter the string ");
scanf("%s",string);
for(int i=0;string[i]!='\0';i++)
{
if(string[i]=='a'||string[i]=='A'||string[i]=='e'||
string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||
string[i]=='O'||string[i]=='u'||string[i]=='U')
count++;
}
printf("The number of vowels in the string are %d\n",count);

return 0;
}

Q3. Write a program to reverse a string and check given string is a


palindrome or not.
Answer:
#include<stdio.h>
#include<string.h>
int main()
{
int size,count=0,c=0,i;
printf("Enter the size ");
scanf("%d",&size);
char string[size],copy[size];
printf("Enter the string ");
scanf("%s",string);
for(int i=0;string[i]!='\0';i++)
{
count++;
}
printf("The reversed string is ");
for(int i=count;i>=0;i--)
{
printf("%c",string[i]);
}
printf("\n");

for(i=0;i<count/2;i++)
{
if(string[i]==string[count-i-1])
c++;

}
if(c==i)
printf("String is palindrome \n");
else
printf("string is not palindrome\n");
return 0;
}

Q4. Write a program to compare two string and concatenate two strings.
Answer:
#include<stdio.h>
#include<string.h>
int main()
{
int size1,size2;
printf("Enter the size of first string ");
scanf("%d",&size1);
printf("Enter the size of second string ");
scanf("%d",&size2);
char string1[size1],string2[size2];
printf("Enter the first string ");
scanf("%s",string1);
printf("Enter the second string ");
scanf("%s",string2);
if(strcmp(string1,string2)==0)
printf("The strings are same \n");
else
printf("The strings are not same \n");
printf("The catenated string is
%s\n",strcat(string1,string2));
return 0;
}

Q5. Write a program to convert a string from lower case to upper case
and vice-versa.
Answer:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int size;
printf("Enter the size of the string ");
scanf("%d",&size);
char string[size];
printf("Enter the string ");
scanf("%s",string);
for(int i=0;i<20;i++)
{
if(islower(string[i])==1)
printf("%c",toupper(string[i]));
else
if(isupper(string[i])==1)
printf("%c",tolower(string[i]));
}
printf("\n");
return 0;
}

You might also like