6(b) Write a C program to determine if the given string is a palindrome or not
Algorithm
Checking whether a given string is Palindrome or not
1. Start
2. Read string
3. Set i:=0,j:=0,flag:=0
4. Repeat step 5 until string[i] == ’\0’, goto step 6.
5. Set i:=i+1
6. Set length:=i
7. Set i:=length -1.
8. Repeat steps 9 to 11 until i < 0, goto step 12
9. Set newstring[j]=string[i]
10. Set j:=j + 1
11. Set i:=i - 1
12. Set newstring[j]:=’\0’
13. Set i:=0
14. Repeat steps 14 to 15 until i >=length, goto step 16
15. If newstring[i] != string[i] set flag:=1
16. Set i:=i+1
17. If flag:=1 write String is not palindrome,goto step 18
18. Write string is palindrome
19. Stop
Flowchart
Algorithm
Checking whether a given string is Palindrome or not
1. Start
2. Read string
3. Set i:=0,j:=0,flag:=0
4. Repeat step 5 until string[i] == ’\0’, goto step 6.
5. Set i:=i+1
6. Set length:=i
7. Set i:=length -1.
8. Repeat steps 9 to 11 until i < 0, goto step 12
9. Set newstring[j]=string[i]
10. Set j:=j + 1
11. Set i:=i - 1
12. Set newstring[j]:=’\0’
13. Set i:=0
14. Repeat steps 14 to 15 until i >=length, goto step 16
15. If newstring[i] != string[i] set flag:=1
16. Set i:=i+1
17. If flag:=1 write String is not palindrome,goto step 18
18. Write string is palindrome
19. Stop
Flowchart
Program
#include<stdio.h>
main()
{
char a[50],b[50];
int length=0,i=0,j=0,flag;
printf("Enter The String:");
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
length=i;
printf("String Length is:%d",length);
for(i=length-1;i>=0;i--)
{
b[j]=a[i];
j++;
}
b[j]='\0';
for(i=0;i<length;i++)
{
if(b[i]==a[i])
flag=0;
else
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Not Palindrome");
printf("\nThe Reverse String of %s is %s",a,b);
}

Comments
Post a Comment