- #1
carl123
- 56
- 0
This is what I did but I'm getting an infinite loop of the reversed string whenever I run the program. I don't know why
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* for fork() and getpid() */
#include <sys/types.h>
#include <iostream>
using namespace std;
string result;
void print(string s, int index){
pid_t pid;
pid = fork();
if(pid == 0){
//child
cout<<s[index];
index--;
print(s,index);
exit(0);
}
else if (pid < 0){
cout<<s[index];
index--;
print(s,index);
exit(0);
}
else{
cout<<s[index];
index--;
exit(0);
}
}
int main(){
string s;
int length;
cout<<"Enter string to invert\n";
cin>>s;
cout<<"Reverse String: \n";
print(s,s.size()-1);
}
Last edited by a moderator: