|
|
|
|
Posted 2004-11-09, 01:41 PM
|
|
|
|
Can someone help me edit my program I do not know how to change to code from C++ to C.
#include <iostream.h>
#include <conio.h>
#include <string.h>
using namespace std;
//---------------------------------------------------------------------------
bool Palindrome(int first, int last, char inputString[]);
int main()
{
int first = 0;// first index for array
int last = 0;// last index for array
char inputString[100];//initialize size of array
cout << "This program determines whether the word"
<< endl
<< "or sentence entered by user is a palindrome."
<< endl;
cout << "\n\nPlease enter a word or phrase: ";
cin >> inputString;//receive input from user
last = strlen(inputString);//store input into character array
if (Palindrome(first,last - 1,inputString)== true)
cout << "Input is classified as a palindrome.";
else
cout << "Input is not classifed as a palindrome.";
getch();
return 0;
}
bool Palindrome(int first, int last, char *inputString)
{
//if the next first index is greater than the next last index
//program concludes that all the elements in array have been compared
//and therefore input should be a palindrome retuned as true.
if (first > last)
return true;
//if next first element and next last element are not the same
//therefore word is not a palindrome returned as false
if (inputString[first]!= inputString[last])
return false;
//if next first element and next last element are same
//call Palindrome function again, increment first element by one
//decrement last element by one to compare if those two elements
//in array are equal.Continue this process until next first element
//and next element are not equal.
else if (inputString[first] == inputString[last])
{
return Palindrome((first+1),(last-1),inputString);
}
}
|
|
|
|
|
|
|
|
|
|
|