From 6d10b5fc4b9d5170457f38dfe5a9f58dcfe84736 Mon Sep 17 00:00:00 2001 From: ash-agarwal Date: Thu, 1 Oct 2020 17:13:21 +0530 Subject: [PATCH] Added program to remove odd indexed characters from strings. --- Strings/RemoveOddIndexedCharacters | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Strings/RemoveOddIndexedCharacters diff --git a/Strings/RemoveOddIndexedCharacters b/Strings/RemoveOddIndexedCharacters new file mode 100644 index 0000000..8808b0f --- /dev/null +++ b/Strings/RemoveOddIndexedCharacters @@ -0,0 +1,40 @@ +#include +using namespace std; + +// Function to remove the odd +// indexed characters from a given string + +string removeOddIndexCharacters(string s) +{ + + // Stores the resultant string + string new_string = ""; + + for (int i = 0; i < s.length(); i++) { + + // If current index is odd + if (i % 2 == 1) { + + // Skip the character + continue; + } + + // Otherwise, append the + // character + new_string += s[i]; + } + + // Return the result + return new_string; +} + +// Driver Code +int main() +{ + string str = "abcdef"; + + // Function call + cout << removeOddIndexCharacters(str); + + return 0; +}