Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e54b1c
commit e04261c
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
*@author: profgrammer | ||
*@date: 30-12-2018 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
int main() { | ||
string s; | ||
cin>>s; | ||
|
||
// method 1: print the string in reverse order | ||
cout<<"The reversed string is: "; | ||
for(int i = s.size()-1;i >= 0;i--) cout<<s[i]; | ||
cout<<endl; | ||
|
||
// method 2: swap characters at either end | ||
string s1 = s; | ||
for(int i = 0, j = s1.size()-1;i <= j;i++, j--){ | ||
char temp = s1[i]; | ||
s1[i] = s1[j]; | ||
s1[j] = temp; | ||
} | ||
cout<<"The reversed string is: "<<s1<<endl; | ||
|
||
// method 3: library functions | ||
s1 = s; | ||
reverse(s1.begin(), s1.end()); | ||
cout<<"The reversed string is: "<<s1<<endl; | ||
|
||
// to check if the string is a palindrome, we need to check if it is equal to its reverse. | ||
|
||
if(s.compare(s1) == 0) cout<<"The string is a palindrome"<<endl; | ||
else cout<<"The string is not a palindrome\n"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
*@author: profgrammer | ||
*@date: 30-12-2018 | ||
*/ | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() { | ||
string s1, s2; | ||
cin>>s1>>s2; | ||
cout<<"The hamming distance is : "; | ||
// the hamming distance is calculated for strings of EQUAL length. hence if the lengths differ we return -1 as an invalid case. | ||
if(s1.size() != s2.size()) cout<<-1<<endl; | ||
else{ | ||
// iterate through both strings and find the number of indices where the strings differ. the total of these indices is the hamming distance | ||
int hammingDistance = 0; | ||
for(int i = 0;i < s1.size();i++){ | ||
if(s1[i] != s2[i]) hammingDistance++; | ||
} | ||
cout<<hammingDistance<<endl; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters