File tree Expand file tree Collapse file tree 4 files changed +132
-1
lines changed
Expand file tree Collapse file tree 4 files changed +132
-1
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ *@author: profgrammer
3+ *@date: 30-12-2018
4+ */
5+
6+ #include < bits/stdc++.h>
7+ using namespace std ;
8+
9+
10+ int main () {
11+ string s;
12+ cin>>s;
13+
14+ // method 1: print the string in reverse order
15+ cout<<" The reversed string is: " ;
16+ for (int i = s.size ()-1 ;i >= 0 ;i--) cout<<s[i];
17+ cout<<endl;
18+
19+ // method 2: swap characters at either end
20+ string s1 = s;
21+ for (int i = 0 , j = s1.size ()-1 ;i <= j;i++, j--){
22+ char temp = s1[i];
23+ s1[i] = s1[j];
24+ s1[j] = temp;
25+ }
26+ cout<<" The reversed string is: " <<s1<<endl;
27+
28+ // method 3: library functions
29+ s1 = s;
30+ reverse (s1.begin (), s1.end ());
31+ cout<<" The reversed string is: " <<s1<<endl;
32+
33+ // to check if the string is a palindrome, we need to check if it is equal to its reverse.
34+
35+ if (s.compare (s1) == 0 ) cout<<" The string is a palindrome" <<endl;
36+ else cout<<" The string is not a palindrome\n " ;
37+
38+ }
Original file line number Diff line number Diff line change @@ -255,6 +255,47 @@ return 0;
255255}
256256```
257257
258+ ###[ Solution by @profgrammer ] ( ./C++/profgrammer_reversepalindrome.cpp )
259+ ``` c
260+ /*
261+ *@author: profgrammer
262+ *@date: 30-12-2018
263+ */
264+
265+ #include < bits/stdc++.h>
266+ using namespace std;
267+
268+
269+ int main () {
270+ string s;
271+ cin>>s;
272+
273+ // method 1: print the string in reverse order
274+ cout<<"The reversed string is: ";
275+ for(int i = s.size()-1;i >= 0;i--) cout<<s[ i] ;
276+ cout<<endl;
277+
278+ // method 2: swap characters at either end
279+ string s1 = s;
280+ for(int i = 0, j = s1.size()-1;i <= j;i++, j--){
281+ char temp = s1[ i] ;
282+ s1[ i] = s1[ j] ;
283+ s1[ j] = temp;
284+ }
285+ cout<<"The reversed string is: "<<s1<<endl;
286+
287+ // method 3: library functions
288+ s1 = s;
289+ reverse (s1.begin(), s1.end());
290+ cout<<"The reversed string is: "<<s1<<endl;
291+
292+ // to check if the string is a palindrome, we need to check if it is equal to its reverse.
293+
294+ if(s.compare(s1) == 0) cout<<"The string is a palindrome"<<endl;
295+ else cout<<"The string is not a palindrome\n";
296+ }
297+ ```
298+
258299### [ Solution by @divyakhetan ] ( ./C++/reverseday2.cpp )
259300
260301``` cpp
Original file line number Diff line number Diff line change 1+ /*
2+ *@author: profgrammer
3+ *@date: 30-12-2018
4+ */
5+
6+
7+ #include < bits/stdc++.h>
8+ using namespace std ;
9+
10+ int main () {
11+ string s1, s2;
12+ cin>>s1>>s2;
13+ cout<<" The hamming distance is : " ;
14+ // the hamming distance is calculated for strings of EQUAL length. hence if the lengths differ we return -1 as an invalid case.
15+ if (s1.size () != s2.size ()) cout<<-1 <<endl;
16+ else {
17+ // iterate through both strings and find the number of indices where the strings differ. the total of these indices is the hamming distance
18+ int hammingDistance = 0 ;
19+ for (int i = 0 ;i < s1.size ();i++){
20+ if (s1[i] != s2[i]) hammingDistance++;
21+ }
22+ cout<<hammingDistance<<endl;
23+ }
24+
25+ }
Original file line number Diff line number Diff line change @@ -173,6 +173,34 @@ int main(){
173173}
174174```
175175
176+ ###[ Solution by @profgrammer ] ( ./C++/profgrammer_hammingdistance.cpp )
177+ ``` c
178+ /*
179+ *@author: profgrammer
180+ *@date: 30-12-2018
181+ */
182+
183+
184+ #include < bits/stdc++.h>
185+ using namespace std;
186+
187+ int main () {
188+ string s1, s2;
189+ cin>>s1>>s2;
190+ cout<<"The hamming distance is : ";
191+ // the hamming distance is calculated for strings of EQUAL length. hence if the lengths differ we return -1 as an invalid case.
192+ if(s1.size() != s2.size()) cout<<-1<<endl;
193+ else{
194+ // iterate through both strings and find the number of indices where the strings differ. the total of these indices is the hamming distance
195+ int hammingDistance = 0;
196+ for(int i = 0;i < s1.size();i++){
197+ if(s1[ i] != s2[ i] ) hammingDistance++;
198+ }
199+ cout<<hammingDistance<<endl;
200+ }
201+ }
202+ ```
203+
176204### [ hamingDistance] ( ./C++/hammingDistanceday3.cpp )
177205
178206``` cpp
@@ -199,7 +227,6 @@ int main(){
199227 }
200228 return 0;
201229}
202- ```
203230
204231## C Implementation
205232
You can’t perform that action at this time.
0 commit comments