ATTENTION!!!: This assignment is for a basic and introductory C++ course. As suc
ID: 3836514 • Letter: A
Question
ATTENTION!!!: This assignment is for a basic and introductory C++ course. As such, please do not use complex or obscure opperations and headers that would typically be used in advanced classes as we have not learned them yet. Use only very simple functions and headers. If you are unsure of whether a technique is too advanced, dont use it. If you can answer the question without using a function, please do so.
"C type" string is implied to mean an array of characters with an end of string mark following the last character.
Write a function named GetLast that will return the last index position a character occupies in a “C type” string as an integer. The string will be the first parameter to the function; the character will be the second. First index position is zero; if not found return -1. The lookup is not case sensitive.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
int GetLast(char a[], char ch){
int index = -1;
int i=0;
while(a[i]!=''){
if(toupper(a[i]) == toupper(ch)) {
index = i;
}
i++;
}
return index;
}
int main()
{
char a[] = "abcbdebdcbsbabc";
char ch = 'A';
cout<<"Position: "<<GetLast(a,ch)<<endl;
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Position: 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.