#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
usingnamespace std;
//List function prototypes below
int lastIndexOf(constchar*, char);
void reverseCString(char*);
bool isPalindrome(constchar*);
void toUpper(char*);
int numLetters(constchar*);
int main()
{
int menuChoice;
char letter, response;
char str1[] = "0123456789a1A";
char str2[] = "Reverse";
char str3[] = "Flower";
char str4[] = "wertytre";
char str5[] = "radar";
char str6[] = "abc ba";
char str7[] = "ra ar";
char str8[] = "abccBa";
char str9[] = "abcdefgha";
do{
//Interface that pops up when the user runs the program so he can choose an option
cout << "---------------------------------------------------------------------" << endl;
cout << " C - STYLE PROCESSING WITH NO STRING OBJECT!" << endl;
cout << "---------------------------------------------------------------------" << endl;
cout << "1. Finding the last index of a number or integer (Entered by user)" << endl;
cout << "2. Reverse char array" << endl;
cout << "3. Detecting the number of replacement" << endl;
cout << "4. Determine if the string is a palindrome" << endl;
cout << "5. Convert a user Defined string to uppercase" << endl;
cout << "6. Display the amount of letters in an array" << endl;
cout << "7. Exit program" << endl;
cout << endl;
//Prompts user to select an option
cout << "Enter your choice: ";
cin >> menuChoice;
// a sample validation routine to try catch input error
while (!cin || menuChoice > 10 || menuChoice < 1)
{
cout<<"INVALID CHOICE ...please retype"<<endl;
cin.clear();
cin.ignore();
cin >> menuChoice;
}
//Interface and function calls with arguments that do the process for every option
switch(menuChoice)
{
case 1: { cout << endl;
cout << "Enter a letter to know the last index of in the string " << str1 << ": ";
cin >> letter;
int index = lastIndexOf(str1, letter);
if (index == -1 & index != toupper(letter))
cout << "The last index in the cstring " << str1 << " with the character '" << letter << "' is " << "-1" << endl;
else
cout << "The last index in the cstring " << str1 << " with the character '" << letter << "' is " << index << endl;
cout << endl;
}
break;
case 2:
cout << endl;
cout << "The original string is " << str2 << "." << endl;
reverseCString(str2);
cout << "The reversed string is " << str2 << endl;
cout << endl;
cout << "The original string is " << str3 << "." << endl;
reverseCString(str3);
cout << "The reversed string is " << str3 << endl;
cout << endl;
break;
case 3:
break;
case 4:{
if(isPalindrome(str4))
{
cout << "The string is a palindrome" << endl;
}
else
{
cout << "The string is not a palindrome" << endl;
}
if(isPalindrome(str5))
{
cout << "The string is a palindrome" << endl;
}
else
{
cout << "The string is not a palindrome" << endl;
}
}
break;
case 5:
break;
case 6: cout << endl;
cout << str1 << " has this many letters: " << numLetters(str1) << endl;
cout << endl;
break;
case 7:cout<<endl;
cout<<"Array processing test now concluded. Exiting program ....."<<endl; //Input validation to exit program
break;
default: cout<<"INVALID CHOICE ...please retype"<<endl; //Input validation when the user enters an integer not within the range of 1-10
break;
}
//Prompts user if they would like to run the program again
cout << "Type Y to run the program again or any other key to exit: ";
cin >> response;
cout << endl;
//printLetters(strHolder);
}while(response == 'Y' || response == 'y');
cout << "Now exiting the c-style processing program ....." << endl; //Terminating message
return 0;
}
int lastIndexOf(constchar* inString, char target)
{
int index = -1;
for (int i = 0; i < strlen(inString); i++)
if (inString[i] == target)
index = i;
return index;
}
void reverseCString(char *inString)
{
int i = 0,j = strlen(inString) - 1;
int temp;
while(i < j)
{
temp = inString[i];
inString[i] = inString[j];
inString[j] = temp;
i++;
j--;
}
}
bool isPalindrome(constchar* inString)
{
int startIndex = 0, endIndex = strlen(inString) + 1;
while(startIndex < endIndex)
{
if(!inString[startIndex] == inString[endIndex] == false)
returnfalse;
startIndex++;
endIndex--;
}
returntrue;
}
/*void toUpper(char* inString)
{
if(inString == NULL) return;
if(inString[0] >= 'a' && inString[0] <= 'z') inString[0] += (char)('A' - 'a');
} */
int numLetters(constchar* inString)
{
int letterCount = 0;
while(*inString != '\0')
{
if(isalpha(*inString))
letterCount++;
inString++;
}
return letterCount;
}
Why I have a feeling that you don't mean the entire function?
Lets take a simple string: "X".
That has one character. The C-string is an array of two characters: {'X', 0}
The strlen() of that C-string is 1. Therefore, endIndex == 2
The valid indices of that array are 0 and 1. 2 is not valid. It is an out-of-range error.
How do you evaluate ! aa == bb == false?
Which operator has higher priority, the not (!) or the equal (==)? In other words, is
1 2 3 4 5
! aa == bb
// same as
(! aa) == bb
// or same as
! (aa == bb)
What is associativity of ==, is
1 2 3 4 5
aa == bb == cc
// same as
(aa == bb) == cc
// or same as
aa == (bb == cc)
Did your instructor really give you code full of logical errors and bits that require deep knowledge of operators?