Skip to content

Instantly share code, notes, and snippets.

@jsstrn
Last active September 14, 2023 18:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsstrn/a087d4b72f8be70771fc to your computer and use it in GitHub Desktop.
Save jsstrn/a087d4b72f8be70771fc to your computer and use it in GitHub Desktop.
These are my solutions to the Coderbyte challenges written in C++
#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;
}
#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;
}
#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