Last active
September 14, 2023 18:56
-
-
Save jsstrn/a087d4b72f8be70771fc to your computer and use it in GitHub Desktop.
These are my solutions to the Coderbyte challenges written in C++
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
int FirstFactorial(int num) { | |
// set the limit to the number provided | |
int limit = num; | |
for (int i = 1; i < limit; ++i) { | |
num = num * i; | |
} | |
return num; | |
} | |
int main() { | |
cout << FirstFactorial(4); | |
return 0; | |
} |
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
string FirstReverse(string str) { | |
string rstr; | |
// loop through characters in str | |
for (int i = 0; i < str.size(); ++i) { | |
// add characters to the front of rstr | |
rstr = str[i] + rstr; | |
} | |
return rstr; | |
} | |
int main() { | |
cout << FirstReverse("reverse me if you can") << endl; | |
// result: "nac uoy fi em esrever" | |
return 0; | |
} |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <string> | |
using namespace std; | |
string LongestWord(string sen) { | |
// create an empty vector | |
vector<string> list; | |
// create an empty string | |
string word = ""; | |
for (int i = 0; i < sen.size(); ++i) { | |
// if the letter is an alphabet add it to word | |
if (isalpha(sen[i])) { | |
word = word + (sen[i]); | |
} | |
// if it isn't a letter add word to list | |
else { | |
list.push_back(word); | |
word = ""; // make word empty | |
} | |
} | |
// if word is not empty then add it to list | |
if (word != "") { | |
list.push_back(word); | |
word = ""; // make word empty | |
} | |
// create a string to store the longest word | |
string longest = ""; | |
for (int i = 0; i < list.size(); ++i) { | |
// compare length of two words | |
if (longest.size() < list[i].size()) { | |
longest = list[i]; | |
} | |
} | |
return longest; | |
} | |
int main() { | |
cout << LongestWord("Merry Christmas to one and all"); | |
// result: Christmas | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment