Help developing this function

Mar 8, 2020 at 1:12am
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>

using namespace std;

//List function prototypes below
int lastOfIndex(const char*, char);

int main()
{
    char str1[] = "Hello World!";
    char str2[] = "C++ can be very interesting!";

    return 0;
}

int lastOfIndex(const char* inString, char target)
{
    inString = target;
}


The output so should be something like this:

Testing the lastOfINdex() function.

The last index in the cstring 0123456789a1 with the character '0' is 0

The last index in the cstring 0123456789a1 with the letter 'a' is 10

The last index in the cstring 0123456789a1 with the character '1' is 11

The last index in the cstring 0123456789a1 with the letter 'x' is -1
Last edited on Mar 8, 2020 at 1:13am
Mar 8, 2020 at 1:24am
In main(), you need to call lastOfIndex(), but you don't.

lastOfIndex() doesn't do anything, as it stands now, and shouldn't even compile.

If you can describe in words what you want it to do, maybe someone can help translate that to C++.
Mar 8, 2020 at 1:28am
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?
Last edited on Mar 8, 2020 at 1:29am
Mar 8, 2020 at 2:37am
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?
Last edited on Mar 8, 2020 at 2:37am
Mar 8, 2020 at 3:09am
This is what I have so far, how can I make it show every letter in the index its located in like for example " 'e' is 1"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
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(const char* 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;
       }
}
Last edited on Mar 8, 2020 at 3:09am
Mar 8, 2020 at 4:42am
1
2
3
            if (inString[i] == target)
            index = i;
            return index;

Braces are really important, and indentation means nothing to the compiler.

Your return statement has nothing to do with your if statement.

You also need a return statement at the end of the function, in case no character is found.
Mar 8, 2020 at 5:21am
I updated it :), had the return index so it terminates in main if the letter entered by user was not there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#include <iostream>
#include <cstring>

using namespace std;

//List function prototypes below
int lastIndexOf(const char*, 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(const char* 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)
{

}
Last edited on Mar 8, 2020 at 5:22am
Topic archived. No new replies allowed.