I tried using the modulo to skip the chars but I doesnt work in some cases.
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops.
Consider the following iterative function that prints an array of characters backward:
#include #include // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); }
Iterative loops have one set of local variables that come and go with each cycle -- the previous cycles' values for these variables are completely forgotten and only the present matters (in the above case, this refers to the counter variable i). The cycles are executed in series. But in recursive loops, the cycles all coexist at the same time, each with their own set of local variables. The cycles are executed in parallel. Parallel in this case means simultaneously, not independently, unless parallelism is explicitly used.
We will be using this property of recursion later in the course on more complex data structures. This exercise serves to reinforce your understanding of the simplest form of recursion -- functions that call themselves.
Assignment
Using the above iterative version of writeArrayBackward as a reference, write a recursive version of this function, but with a couple of twists:
1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below).
void writeArrayNthBackward(const char [], int, int, int);
The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n == 1, then the string is printed normally in reverse (no characters are dropped). If n == 2, then every "other" character is printed, and so on.
You can constrain N to be 1 <= N <= 3.
Your code does NOT need to output a newline ('endl') within the recursive function itself (as the above iterative version does), you can output the newline after the return of the function.
2) If any of the removed characters in the string were numbers (not letters), output their sum. For example, if your function removed the numbers 3 and 1 from the string 'abc123', the result would be 3 plus 1, which is 4. See the below table for more examples..
Converting a String Digit to an Integer
If you have a string and want to convert a character in the string to an int, there are a few ways to do it. The easiest is probably this one:
char *a = "123"; int n; n = (int)a[0] - '0';
Notice we can't simply cast a[0] to an int, because that will give us the ASCII code for the character '1', which is 49. We want the value of 'n' to be 1, so by subtracting the ASCII code for 0 (which is 48), it "translates" the character to an integer value.
Input string | N | Output |
abc123 | 2 | 2ca (sum of numbers removed: 4) |
hello | 1 | olleh (sum of numbers removed: 0) |
hello | 3 | l (sum of numbers removed: 0) |
c1c1c1 | 2 | ccc (sum of numbers removed: 3) |
PreviousNext
/* remove comments in the code to see all the test cases which are given in the question */
/* save code xyz.cpp and compile using g++ xyz.cpp and run ./a.out you will see the output (I also attached screenshot of output) */
#include<iostream>
#include<cstring>
using namespace std;
int writeArrayNthBackward(const char arr[], int start, int len,
int skip){
if(len<=start)
return 0;
int n=0;char x;
for(int i=len-1; i>len-skip; i--){
x=arr[i];
if(x>='0' && x <=
'9'){
n +=
(int)arr[i]-'0';
}
}
cout<<arr[len-skip];
n += writeArrayNthBackward(arr, start, len-skip,
skip);
return n;
}
int main(){
const char *s = "abc123";
const char *d = "hello";
const char *a = "c1c1c1";
int v;
v=writeArrayNthBackward(s, 0, strlen(s), 2);
cout<<"(sum of numbers removed:
"<<v<<")"<<endl; //uncomment below test cases to
see output of all the testcases
/* v=writeArrayNthBackward(d, 0, strlen(d), 1);
cout<<"(sum of numbers removed:
"<<v<<")"<<endl;
v=writeArrayNthBackward(d, 0, strlen(d), 3);
cout<<"(sum of numbers removed:
"<<v<<")"<<endl;
v=writeArrayNthBackward(a, 0, strlen(a), 2);
cout<<"(sum of numbers removed:
"<<v<<")"<<endl;
*/
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.