Name your program palindrome. cpp All strings in the program are implemented as
ID: 3862087 • Letter: N
Question
Name your program palindrome. cpp All strings in the program are implemented as instances of the C++ string class Prompt for the user to enter a potential palindrome Read the user input in to an instance of the string class (see string I/O for an example) a. The test input will NOT contain punctuation characters (periods, commas, exclamation or question marks), etc. b. The test input will consist of only one case (i.e., it will not contain a mix of upper and lower case letters) c. The test input will contain spaces, which you must remove (see the code fragment below) Test the string to see if it is a palindrome (follow either of the two examples in palnumber.cpp) Write a message to the screen stating that the input was or was not a valid palindrome The program terminates after print the message - no prompts, loops, or pauses Removing Spaces From A String It is probably easiest to first remove all of the spaces from the string before testing it to see if it is a palindrome. The following code fragment (which you may use in your program) will remove the spaces from a string object: for (int i = 0; iExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale>
#include <iostream>
using namespace std;
char palnumber(char *,int);
char* formatstring( char* ip )
{
char* result = NULL ;
char delimiter[] = " " ;
char* new_string = (char*) calloc( strlen( ip ) + 1, sizeof( char ) ) ;
result = strtok( ip, delimiter ) ;
while( result != NULL )
{
cout<< " token: %s"<<result ;
strcat( new_string, result ) ;
result = strtok( NULL, delimiter ) ;
}
cout<< " New string in format functions is "<<new_string;
return new_string ;
}
void makelower( char* ip )
{
// make string lowercase
int i = 0 , string_length = strlen( ip );
while( ip[i] != '' )
{
ip[i] = tolower( ip[i] ) ;
++i ;
}
}
char palnumber( char string[], int length )
{
if( length < 2 )
return 1;
if( string[0] != string[length - 1] )
return 0;
else
return palnumber( string + 1, length -2 );
}
int main()
{
char string[BUFSIZ] = {''} ;
cout<<"enter string to check: ";
fgets( string, BUFSIZ, stdin ) ;
makelower( string ) ;
char* new_string = formatstring( string ) ;
cout<< " "<<new_string ;
int string_length = strlen( new_string ) ;
if( palnumber ( new_string, string_length ) == 1 )
cout<<" valid palnumber ";
else
cout<<" Not valid palnumber ";
return 0 ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.