Hi, I am working on a program that uses c style string processing and I need to use a function called int lastOfIndex(const char* inString, char target) . This function is supposed to find the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. The function should be case sensitive (so 'b' is not a match for 'B').
Here is what I have so far, I am a bit in a pickle of trying to develop this
I have not called it yet because I do not know how to begin this function, it's supposed to return the last index where the char target can be found in the string like the sample output I provided above. It should also be case sensitive so I am thinking if statements would be included as well?
something like
index = strlen() //that's the terminal zero, the true end of string.
iterate backwards, up to strlen() times until you find the char (or if not there).
if its there, return the index. since iterating backwards, the first time you find it is correct.
if its not there, return -1 or something?
int main()
{
char letter;
char str1[] = "Hello World";
char *strHolder;
strHolder = str1;
//char str2[] = "C++ can be very interesting!";
int index = lastIndexOf(strHolder, letter);
if (index == -1)
cout << "Character not found";
else
cout << "The last index in the cstring " << strHolder << " with the character " << letter << " is " << index;
return 0;
//printLetters(strHolder);
return 0;
}
int lastIndexOf(constchar* inString, char target)
{
int index = -1;
for (int i = 0; i < strlen(inString); i++)
{
target = inString[i];
if (inString[i] == target)
index = i;
return index;
}
}
#include <iostream>
#include <cstring>
usingnamespace std;
//List function prototypes below
int lastIndexOf(constchar*, char);
void reverseCString(char*);
int main()
{
int menuChoice;
char letter, response;
char str1[] = "0123456789a1A";
char str2[] = "Reverse";
char *strHolder2;
strHolder2 = str2;
char *strHolder;
strHolder = str1;
//char str2[] = "C++ can be very interesting!";
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. Determine if the string is a palindrome" << endl;
cout << "4. Convert a user Defined string to uppercase" << endl;
cout << "5. Display the amount of letters in an array" << endl;
cout << "6. 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(strHolder, letter);
if (index == -1 & index != toupper(letter))
cout << "The last index in the cstring " << strHolder << " with the character '" << letter << "' is " << "-1" << endl;
else
cout << "The last index in the cstring " << strHolder << " with the character '" << letter << "' is " << index << endl;
cout << endl;
}
break;
case 2: reverseCString(strHolder2);
cout << "the reversed version is " << strHolder2 << endl;
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6: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)
{
}