Question:13
C program to take input for a string and print reverse of the string?
example:
n=”hello”
reverse:
olleh
Sol:
#include<stdio.h>
int main()
{
char n[20];
int i=0;
/* clrscr(); */
printf("Enter any string ");
gets(n);
//to find length
for(i=0;n[i]!='\0';i++);
//
/*
i=0;
while(n[i]!='\0')
{
i++;
}
*/
printf("len = %d\n",i);
i--;
printf("Reverse string is ");
while(i>=0)
{
printf("%c",n[i]);
i--;
}
getch();
return(0);
}
/* Output */ Enter any string hello len = 5 Reverse string is olleh
Question:14
C program to take input for two string and concatenate them into a third string.
n1=”hello”
n2=”world”
concatenated string:
n3=”hello world”
Sol:
#include<stdio.h>
int main()
{
char n1[20],n2[20],n3[40];
int i=0,j=0;
/* clrscr(); */
printf("Enter 1st string ");
gets(n1);
printf("Enter 2nd string ");
gets(n2);
while(n1[i]!='\0') //for(;n1[i]!='\0';)
{
n3[i]=n1[i];
i++;
}
//if we want to give space
n3[i]=' ';
i++;
while(n2[j]!='\0')
{
n3[i]=n2[j];
i++;
j++;
}
n3[i]='\0';
printf("copied string is %s\n",n3);
getch();
return(0);
}
/* Output */ Enter 1st string hello Enter 2nd string world copied string is hello world
Question:15
C program to take input for a string check and print whether the string is palindrome or not.
n=”hello”
reverse : olleh
string is not palindrome
n=”madam”
reverse : madam
string is palindrome
Sol:
#include<stdio.h>
int main()
{
char n[20];
int i=0,j,len,z=0;
/* clrscr(); */
printf("Enter any string ");
gets(n);
//to find length
for(i=0;n[i]!='\0';i++);
len=i;
//printf("len = %d\n",len);
i=0;
j=len-1;
while(i<=len/2)
{
//printf("%c %c\n",n[i],n[j]);
if(n[i]!=n[j])
{
z=1;
break;
}
i++;
j--;
}
if(z==0)
printf("string is palindrome");
else
printf("string is not palindrome");
getch();
return(0);
}
/* Output */ case:1 Enter any string hello string is not palindrome case:2 Enter any string madam string is palindrome




