Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C++ program. In the code. How to use (char *) instead of the (string)? P

ID: 3670064 • Letter: T

Question

This is C++ program.

In the code. How to use (char *) instead of the (string)?

Please change all the string and describe details.

such as in this function void if_b(int b,int k, int m, string &a, string &s, string &r);

I want it change to void if_b(int b,int k, int m, char *a, char *s, char *r);

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;


void if_b(int b,int k, int m, string &a, string &s, string &r);
void total(int ,int , int , string &, string &, string &);


void new_string(int b,int k, int m, string &a, string &s, string &r){
   if (m==3){

       cin.ignore();  
      
       cout<<"Enter New String :";
       getline(cin, a);
       total(b, k ,m, a, s, r);
  
       k=1;      
       }
}


void get_string(int b,int k, int m, string &a, string &s, string &r){
   int n=a.length();
  
   for (int i=0; i<n; i++){
       if (a[i]!=a[n-i-1]){
           b=1;
           break;
       }  
      
       else if (a[i]=a[n-i-1]){
           b=0;
       }  
   }      
   if_b(b,k,m,a,s,r);
}


void if_b(int b, int k, int m, string &a, string &s, string &r){  
  
  
   if(b==1){
       cout<<"This is not palindrome"<<endl;
                  
   }
      
   if (b==0){
       cout<<"This is palindrome"<<endl;
   }
  
       total(b,k,m,a,s,r);
  
  
  
  
}

void replace_s(string &s, string &r){
   cin.ignore();
  
   cout<<"Please input the string which you want to search :";
   getline(cin, s);
   cout<<"Please input the string which you want ro replac: ";
   getline(cin, r);  
}


string replace_a(string &a, string &s, string r){  

   replace_s(s, r);
   if(a.find(s)!=std::string::npos){
       a.replace(a.find(s), s.length(), r);
       return a;  
   }
  
   else{
   cout<<"Invalid Inpus String !! Search String Not found ";
       return "";
   }
  
}  

void total_1(int b,int k, int m, string &a, string &s, string &r){
       if (m==1){
       k=0;
   get_string(b,k,m,a,s,r);
       }
}

void total_2(int b, int k, int m, string &a, string &s, string &r){
       if (m==2){
           k=1;
           replace_a(a, s, r);
           cout<<"Your new string is: "<<a<<endl;          
           total(b,k,m,a,s,r);
       }
      
}

void total_0 (int b, int k, int m){

   if(m==0){
k=1;
cout<<"Bye Bye!"<<endl;
       }

}

void total(int b, int k, int m, string &a, string &s, string &r){  
      
       cout<<"Detemine if palindrome (1), replace string (2), enter new string(3), or quit (0) :";
       cin>>m;
                          
       total_1(b,k,m,a,s,r);          
       total_2(b,k,m,a,s,r);          
       new_string(b,k, m, a, s, r);
       total_0(b,k,m);  
  
}

int main(){

   string a, s, r;
int k=1, m, b;
  
   cout<<"Enter a String :";
getline(cin, a);
   total(b, k, m, a, s, r);
  
return 0;
}

Explanation / Answer

#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

void if_b(int b,int k, int m, char *a, char *s, char *r);
void total(int ,int , int , char *, char *, char *);

void new_string(int b,int k, int m, char *a, char *s, char *r){
if (m==3){
cin.ignore();
  
cout<<"Enter New String :";
cin.getline(a, 256);
total(b, k ,m, a, s, r);
  
k=1;
}
}

void get_string(int b,int k, int m, char *a, char *s, char *r){
int n=strlen(a);
  
for (int i=0; i<n; i++){
if (a[i]!=a[n-i-1]){
b=1;
break;
}
  
else if (a[i]=a[n-i-1]){
b=0;
}
}
if_b(b,k,m,a,s,r);
}

void if_b(int b, int k, int m, char *a, char *s, char *r){
  
  
if(b==1){
cout<<"This is not palindrome"<<endl;
  
}
  
if (b==0){
cout<<"This is palindrome"<<endl;
}
  
total(b,k,m,a,s,r);
  
  
  
  
}
void replace_s(char *s, char *r){
cin.ignore();
  
cout<<"Please input the string which you want to search :";
   cin.getline(s, 256);
cout<<"Please input the string which you want ro replac: ";
cin.getline(r, 256);
}

int find(char *a, char *s){
   int aSize = strlen(a);
   int sSize = strlen(s);
   for(int i = 0; i < aSize; ++i){
       bool found = true;
       for(int j = 0; j < sSize; ++j){
           if(a[i + j] != s[j]){
               found = false;
               break;
           }
       }
       if(found){
           return i;
       }
   }
   return -1;
}

void replace(char *&a, int ind, int length, char *r){
   char *temp = new char[256];
   int rLen = strlen(r);
   int aLen = strlen(a);
   for(int i = 0; i < ind; ++i){
       temp[i] = a[i];
   }
   for(int i = 0; i < rLen; ++i){
       temp[ind + i] = r[i];
   }
   for(int i = 0; a[ind+length+i] != ''; ++i){
       temp[ind+rLen+i] = a[ind+length+i];
   }
   a = temp;
}

string replace_a(char *a, char *s, char *r){
replace_s(s, r);
   int ind;
if((ind = find(a, s)) != -1){
       replace(a, ind, strlen(s), r);
return a;
}
  
else{
cout<<"Invalid Inpus String !! Search String Not found ";
return "";
}
  
}
void total_1(int b,int k, int m, char *a, char *s, char *r){
if (m==1){
k=0;
get_string(b,k,m,a,s,r);
}
}
void total_2(int b, int k, int m, char *a, char *s, char *r){
if (m==2){
k=1;
replace_a(a, s, r);
cout<<"Your new string is: "<<a<<endl;
total(b,k,m,a,s,r);
}
  
}
void total_0 (int b, int k, int m){
if(m==0){
k=1;
cout<<"Bye Bye!"<<endl;
}
}
void total(int b, int k, int m, char *a, char *s, char *r){
  
cout<<"Detemine if palindrome (1), replace string (2), enter new string(3), or quit (0) :";
cin>>m;
  
total_1(b,k,m,a,s,r);
total_2(b,k,m,a,s,r);
new_string(b,k, m, a, s, r);
total_0(b,k,m);
  
}
int main(){

char *a = new char[256], *s = new char[256], *r = new char[256];
int k=1, m, b;
  
cout<<"Enter a String :";
cin.getline(a, 256);
total(b, k, m, a, s, r);
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote