forked from gutty333/Easy-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
69_SnakeCase.cpp
35 lines (30 loc) · 1010 Bytes
/
69_SnakeCase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// For this challenge you will be converting a string into snake case format.
/*
have the function SnakeCase(str) take the str parameter being passed and return it in proper snake case format where each word is lowercased and separated from adjacent words via an underscore. The string will only contain letters and some combination of delimiter punctuation characters separating each word.
For example: if str is "BOB loves-coding" then your program should return the string bob_loves_coding.
*/
#include <iostream>
#include <string>
using namespace std;
string SnakeCase(string str)
{
for (int x = 0; x < str.size(); x++)
{
if (int(str[x]) >= 32 && int(str[x]) <= 47)
{
str[x] = '_';
}
else
{
str[x] = tolower(str[x]);
}
}
return str;
}
int main()
{
cout << SnakeCase("BOB loves-coding") << endl; // bob_loves_coding
cout << SnakeCase("cats AND*Dogs-are Awesome") << endl; // cats_and_dogs_are_awesome
cout << SnakeCase("a b c d-e-f%g") << endl; // a_b_c_d_e_f_g
return 0;
}