The function of program is to verify if string is a palindrome while extracting punctuation. The code extracts double quotes and single quotes but I cant seem to figure out how to extract spaces. When watching array value with spaces from debugger it seems as if the string is being truncated where the spaces were inserted. I input ra da r from keyboard and the array value of string is ra in debugger watch list.
#include "stdafx.h"
#include <iostream>
bool palindrome(char[], int);
int main()
{
using namespace std;
bool status;
int siz,i;
char strarry[20];
siz = 0;
cout << " enter string" << endl;
cin >> strarry;
for (i = 0; strarry[i] != '\0'; i++)
++siz;
status = palindrome(strarry, siz);
if (status)
cout << " the string is a palindrome" << endl;
else
cout << "not a palindrome" << endl;
cin.clear();
cin.ignore(255, '/n');
cin.get();
return 0;
}
bool palindrome(char arry[], int size)
{
int i, l;
char arryn, arrysize;
static bool search = false;
static bool match = false;
static bool found = false;
static int n = 0;
int end = size - 1;
if (search != true){
for (i = 0; i <= end; i++){
if ((arry[i] == ' ') || (arry[i] == '\'') || (arry[i] == '\"')){
if ((end - i) >= 1){
for (l = i; l <= end; l++)
arry[l] = arry[l + 1];
--end;
size = end;
}
else{
size= end - 1;
}
found = true;
}//end if
}// end for
if (found == false)
size = end;
search = true;
}//end if
if (((n - size) <= 1) && match )
return true;
else{
arryn = arry[n];
arrysize = arry[size];
if (arry[n] == arry[size]){
++n;
match = true;
return palindrome(arry, size - 1);
}
else{
match = false;
return false;
}//end else
}// end else
}
Aucun commentaire:
Enregistrer un commentaire