diff --git a/answers/q1.cpp b/answers/q1.cpp new file mode 100644 index 0000000..0fd2c02 --- /dev/null +++ b/answers/q1.cpp @@ -0,0 +1,38 @@ +//1. Print Hello world! in reverse + +#include + +using namespace std; + +string wordReverse(string str) +{ + int i = str.length() - 1; + int start, end = i + 1; + string result = ""; + + while (i >= 0) { + if (str[i] == ' ') { + start = i + 1; + while (start != end) + result += str[start++]; + + result += ' '; + + end = i; + } + i--; + } + start = 0; + while (start != end) + result += str[start++]; + + return result; +} +int main() +{ + string str = "Hello World"; + + cout << wordReverse(str); + + return 0; +} \ No newline at end of file diff --git a/answers/q2.cpp b/answers/q2.cpp new file mode 100644 index 0000000..e69de29