C program String Reverse and palindrome
A array of characters enclosed by double quote and delimited by a NULL character is called string. The string is transformed into reverse string without using build-in function by this c programming given below.
char name[20] = "dennis ritchie";
String Reverse
string reverse - read a character by character from last to first index and copy it into another character array and finally add NULL character (delimiter) at end of the string
------------ "dennis ritchie" ----> | Reverse | ---> "eihctir sinned" ------------
String Reverse - C programming code
The C program transform a given string into reverse string. The input string is to reverse read from user and prints reversed string as result of the program.
#include <stdio.h>
#include <string.h>
int main() {
char str[20];
char rstr[20];
printf("\n Reverse a given string without using build-in function");
printf("\n Enter a string :");
scanf("%s",&str) ;
int len = strlen(str);
for(int n=0;n<len;n++)
rstr[len-1-n]=str[n];
rstr[len] = '\0';
printf("\n Reversed String : %s",rstr);
return 0;
}
String Reverse - C programming output
Reverse a given string without using build-in function Enter a string government Reversed String tnemnrevog
It is a C program which verifies that the given string is palindrome or not without using build-in function string reverse.
palindrome
A string is a palindrome or not is found by comparing the string with reverse of the string itself. if the comparison result is true, the string is called palindrome otherwise the string is not palindrome.
------------ "dennis ritchie" ----> | Reverse | ----> "eihctir sinned" | ------------ | | | | ---------- | |----> | compare |<--| ---------- | | palindrome/ not
Palindrome - C programming code
The C program transform a given string into reverse string and compare both the given string and reversed string are equal. The input string is read from user and prints the given string is palindrome or not as result of the program.
#include <stdio.h>
#include <string.h>
int main() {
char str[20];
char rstr[20];
printf("\n Finds a given string is Palindrome or not quot;);
printf("\n Enter a string :");
scanf("%s",&str) ;
int len = strlen(str);
for(int n=0;n<len;n++)
rstr[len-1-n]=str[n];
rstr[len] = '\0';
if ( strcmp(str,rstr)==0 )
{
printf("\nThe string is Palindrome");
printf ("\n %s",str );
printf ("\n %s",rstr );
}
else {
printf("\n The string is not Palindrome");
}
return 0;
}
Palindrome - C programming output
Finds a given string is Palindrome or not Enter a string dad The string is Palindrome dad Finds a given string is Palindrome or not Enter a string sister The string is not Palindrome retsis
Comments
Post a Comment