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

This c++ program. It used to check if user types is a palindrome. Thers is 2 prb

ID: 3670832 • Letter: T

Question

This c++ program. It used to check if user types is a palindrome.

Thers is 2 prblems.

I need keep chat * function, please don't say just use string instead it.

Please double check my questions.

1. I misunderstand palindrome. I make a function get_string(int b,int k, int m, char *a, char *s, char *r), to check if it is palindrome. Maybe my function is wrong or the mistake is in other place. I don't understand what is palindrome. So, please help me fix palindrome check.

2. Please run the program type a string, then please choose 2 (replace). Then you will see the problem that you can't use new string to replace a old string. Maybe the problem is in function void replace(char *&a, int ind, int length, char *r) or in other function.

Please check the output after you fix my code. Thank you.

#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;
}

Explanation / Answer

#include<iostream>
using namespace std;

int main(){
char string1[20];
int i, length;
int flag = 0;
  
cout << "Enter a string: ";
cin >> string1;
  
length = strlen(string1);
  
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
   }
   }
  
if (flag) {
cout << string1 << " is not a palindrome" << endl;
}
else {
cout << string1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}

make this code ur primar one this code will satisfy your needs.

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