please trace and debug the following functions in c ++ : // recursive print of a
ID: 3548925 • Letter: P
Question
please trace and debug the following functions in c ++ :
// recursive print of a string
void print(char* s){
if(*s==____________)
return;
cout<<*s;
print(______________);
}
void main(){
char a[6]="hello";
print(a);
}
// recursive reverse print of a string
void rev_print(char* s){
if(*s==____________)
return;
____________________________;
___________________________;
}
void main(){
char a[100];
cin>>a;
rev_print(a);
}
// recursive reverse print of an integer
void rev_print(int n){
if(n/10==0){
cout<<n%10;
return;
}
cout<<__________________________;
rev_print(___________________);
}
void main(){
int a;
cin>>a;
rev_print(a);
}
// recursive print of an integer
void print(int n){
if(n/10==0){
cout<<n%10;
return;
}
print(_______________);
cout<<___________________;
}
void main(){
int a;
cin>>a;
print(a);
}
Explanation / Answer
// please trace and debug the following functions in c ++ :
// recursive print of a string
void print(char* s){
if(*s==0) // check for ending of string it is null i.e 0 break recursion.
return;
cout<<*s; // print character
print(++s); // call same function with next character onwards.
}
void main(){
char a[6]="hello";
print(a);
}
// recursive reverse print of a string
void rev_print(char* s){
if(*s==0) // check for ending of string it is null i.e 0 break recursion.
return;
rev_print(++s); // call same function with next character onwards. to print string in reverse.
cout<<*(--s); // print character
}
void main(){
char a[100];
cin>>a;
rev_print(a);
}
// recursive reverse print of an integer
void rev_print(int n){
if(n/10==0){ // if this is last digit in string print digit and break recursion.
cout<<n%10;
return;
}
cout<<n%10; // print digit.
rev_print(n/10); // call function with the printed digit removed.
}
void main(){
int a;
cin>>a;
rev_print(a);
}
// recursive print of an integer
void print(int n){
if(n/10==0){ // if this is last digit in string print digit and break recursion.
cout<<n%10;
return;
}
print(n/10); // call function with the last digit removed. for reverse printing.
cout<<n%10; // print digit.
}
void main(){
int a;
cin>>a;
print(a);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.