1) To find the length of the string, Reverse a given string and check
palindrome or not.
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s1[20], s2[20];
int n;
cout<<"Enter a string: ";
[Link](s1,20);
n=strlen(s1);
cout<<"Reversed string: ";
for(int i=0;i<n/2;i++)
swap(s1[i],s1[n-i-1]);
cout<<s1<<endl;
if (strcmp(s1,s2)==0)
cout<<"The given string is palindrome"<<endl;
else
cout<<"The given string is not palindrome"<<endl;
}
Output:
Enter a string: hello
Reversed string: olleh
The given string is not palindrome
2) String Manipulation(Built in Function)
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s1[20], s2[20];
int n;
cout<<"Enter a string: ";
[Link](s1,20);
n=strlen(s1);
cout<<"length of string"<<strlen(s1)<<endl;
strcpy(s2,s1);
cout<<"Copied string is"<<s2<<endl;
strcat(s2,s1); //concatenation of two strings
cout<<"Concatenation of s1 and s2"<<s2<<endl;
cout<<"Reversed string: ";
for(int i=0;i<n/2;i++)
swap(s1[i],s1[n-i-1]);
cout<<s1<<endl;
return 0;
}
Output:
Enter a string: madam
length of string5
Copied string ismadam
Concatenation of s1 and s2madammadam
Reversed string: madam