Skip to content

Commit

Permalink
cpp code for day 2 and 3 (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
profgrammer authored and MadhavBahl committed Dec 31, 2018
1 parent 7e54b1c commit e04261c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Day2/C++/profgrammer_reversepalindrome.cpp
@@ -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";

}
41 changes: 41 additions & 0 deletions Day2/README.md
Expand Up @@ -255,6 +255,47 @@ return 0;
}
```

###[Solution by @profgrammer](./C++/profgrammer_reversepalindrome.cpp)
```c
/*
*@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";
}
```

### [Solution by @divyakhetan](./C++/reverseday2.cpp)

```cpp
Expand Down
25 changes: 25 additions & 0 deletions Day3/C++/profgrammer_hammingdistance.cpp
@@ -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;
}

}
29 changes: 28 additions & 1 deletion Day3/README.md
Expand Up @@ -173,6 +173,34 @@ int main(){
}
```

###[Solution by @profgrammer](./C++/profgrammer_hammingdistance.cpp)
```c
/*
*@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;
}
}
```

### [hamingDistance](./C++/hammingDistanceday3.cpp)

```cpp
Expand All @@ -199,7 +227,6 @@ int main(){
}
return 0;
}
```

## C Implementation

Expand Down

0 comments on commit e04261c

Please sign in to comment.