Skip to content

Commit

Permalink
Added C++ implementation of Day4 (#49)
Browse files Browse the repository at this point in the history
* Added C++ implementation of Day4

* Day4 directory renamed to day4
  • Loading branch information
Razdeep committed Dec 25, 2018
1 parent 64de4be commit 3c7211c
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 2 deletions.
40 changes: 40 additions & 0 deletions day4/C++/NumVowelsPartA.cpp
@@ -0,0 +1,40 @@
/**
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @github: https://github.com/razdeep
* @date: 25/12/2018
**/

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// set count = 0
int count = 0;
string str;
cout << "/* ===== Number of Vowels ===== */" << endl;
cout << "\nEnter the string: ";
cin >> str;

// Convert input string to lower case
// using transform() function and ::tolower in STL
transform(str.begin(), str.end(), str.begin(), ::tolower);

// Run a loop from 0 to string length
for (int i = 0; i < str.length(); i++)
{
if (
str[i] == 'a' ||
str[i] == 'e' ||
str[i] == 'i' ||
str[i] == 'o' ||
str[i] == 'u')
{
count++;
}
}

// Print the result
cout<<"Number of vowels in \""<<str<<"\" = "<<count<<endl;
return 0;
}
42 changes: 42 additions & 0 deletions day4/C++/NumVowelsPartB.cpp
@@ -0,0 +1,42 @@
/**
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @github: https://github.com/razdeep
* @date: 25/12/2018
*
* Max Char Problem Solution
*
**/

#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
using namespace std;
int main()
{
int count = 0;

// Input the string
string str;
cout << "/* ===== Number of Vowels ===== */" << endl;
cout << "\nEnter the string: ";
cin >> str;

// Convert input string to lower case
// using transform() function and ::tolower in STL
transform(str.begin(), str.end(), str.begin(), ::tolower);

int max_count = INT_MIN;
char max_label;
for (int i = 0; i < str.size(); i++)
{
int this_count = std::count(str.begin(), str.end(), str[i]);
if (this_count > max_count)
{
max_count = this_count;
max_label = str[i];
}
}
cout << "'" << (char)max_label << "' has " << max_count << " occurences." << endl;
return 0;
}
99 changes: 97 additions & 2 deletions day4/README.md
Expand Up @@ -215,6 +215,53 @@ public class NumVowels2 {
}
}
```

## C++ Implementation

### [NumVowelsPartA.cpp](./C++/NumVowelsPartA.cpp)

```cpp
/**
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @github: https://github.com/razdeep
* @date: 25/12/2018
**/

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// set count = 0
int count = 0;
string str;
cout << "/* ===== Number of Vowels ===== */" << endl;
cout << "\nEnter the string: ";
cin >> str;

// Convert input string to lower case
// using transform() function and ::tolower in STL
transform(str.begin(), str.end(), str.begin(), ::tolower);

// Run a loop from 0 to string length
for (int i = 0; i < str.length(); i++)
{
if (
str[i] == 'a' ||
str[i] == 'e' ||
str[i] == 'i' ||
str[i] == 'o' ||
str[i] == 'u')
{
count++;
}
}
// Print the result
cout<<"Number of vowels in \""<<str<<"\" = "<<count<<endl;
return 0;
}
```

## Python Implementation

### [Solution](./Python/partA_sol.py)
Expand All @@ -240,7 +287,6 @@ for char in string:
print("Number of vowels in the string are : ",count)

```

<hr/>

## Part B -- Max Chars Problem
Expand Down Expand Up @@ -316,6 +362,56 @@ function maxChars (sentence) {

maxChars('helllllo worlld');
```

## C++ Implementation

### [NumVowelsPartB.cpp](./C++/NumVowelsPartB.cpp)

```cpp
/**
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
* @github: https://github.com/razdeep
* @date: 25/12/2018
*
* Max Char Problem Solution
*
**/

#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
using namespace std;
int main()
{
int count = 0;

// Input the string
string str;
cout << "/* ===== Number of Vowels ===== */" << endl;
cout << "\nEnter the string: ";
cin >> str;

// Convert input string to lower case
// using transform() function and ::tolower in STL
transform(str.begin(), str.end(), str.begin(), ::tolower);

int max_count = INT_MIN;
char max_label;
for (int i = 0; i < str.size(); i++)
{
int this_count = std::count(str.begin(), str.end(), str[i]);
if (this_count > max_count)
{
max_count = this_count;
max_label = str[i];
}
}
cout << "'" << (char)max_label << "' has " << max_count << " occurences." << endl;
return 0;
}
```

## Python Implementation

### [Solution](./Python/partB_sol.py)
Expand Down Expand Up @@ -343,4 +439,3 @@ print("The most occouring character in the string is : ", max(characters,key=cha
```



0 comments on commit 3c7211c

Please sign in to comment.