string functions

need help implementing these duties :

s – search

This option will allow the user to search for a specified string in the worked-on string. If the string is

found, it will display the starting index (position) of the searched string in the worked-on string.

I – insert

This option will allow the user to insert a string at a specified index in the worked-on string.

a – append

This option will allow the user to append (add) a specified string at the end of the worked-on string.

d – delete

This option will search for a specified string in the worked-on string. If the specified string is found, it will

eliminate it from the worked-on string.r – replace

This option will allow the user to replace an existing string in the worked-on string with a specified

string.

e – exit

This option will result in ending the program.


i know i have to use strstr,strcat,strlen, strcpy, strncmp i just dont know where and how to put it in the switch statement.


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

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

int main()
{

    char a_string[80];
    char choices;

    cout << "Enter A String:\n";
    cin.getline(a_string,80);
    cout << "  " << endl;
    cout << "Current String: " << a_string << endl;
    cout << "  " << endl;

    const int search_choice = 's', insert_choice = 'i', append_choice = 'a', delete_choice = 'd', replace_choice = 'r', exit_choice = 'e';


    cout << "   " << endl;
    cout << "s-search" << endl;
    cout << "i-insert" << endl;
    cout << "a-append" << endl;
    cout << "d-delete" << endl;
    cout << "r-replace" << endl;
    cout << "e-exit" << endl;
    cout << "   " << endl;
    cout << "Choose An Option: " << endl;
    cin >> choices;


    switch (choices)
    {
        case search_choice:
            cout << "You Choose: Search";

        break;
        case insert_choice:
            cout << "You Choose: Insert";

        break;
        case append_choice:
            cout << "You Choose: Append";

        break;
        case delete_choice:
            cout << "You Choose: Delete";

        break;
        case replace_choice:
            cout << "You Choose: Replace";

        break;
        case exit_choice:
            cout << "You Choose: Exit";

        break;
        default: cout << "Please Enter A Valid Letter ";
        break;
    }


return 0;

}

Last edited on
maybe you should read the page on switch cases on this website.

switch works like this

1
2
3
4
5
6
7
8
9
switch(the stuff you want to compare with your cases)
{
    case 1
         commands;
    break;
    case 2
         commands;
     break;
}



so just add your string funtions in the commands part
can you show me how to implement the search function as an example.
for the search function

1
2
3
4
5
6

  case search_choice:
            const char * a_string;
            char * p = strstr(a_string);
            p = strstr (a_string);


is this how you would do it ? i am still getting errors though
Last edited on
Are you not allowed to use std::string? All of the functions you're describing have been emplemented in C++. If you're assignment allows you to use C++ (as opposed to C), then you should use std::string.

http://en.cppreference.com/w/cpp/string/basic_string
for this assignment i have to stick to c strings.

here is what i have so far but error still come up : i need help with the exit & append function.

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
 
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

int main()
{

    char a_string[80];
    char choices;
    char dest[80];
    int search (char sourceString [], char targetString [] );


    cout << "Enter A String:\n";
    cin.getline(a_string,80);
    cout << "  " << endl;
    cout << "Current String: " << a_string << endl;
    cout << "  " << endl;

    const int search_choice = 's', insert_choice = 'i', append_choice = 'a', delete_choice = 'd', replace_choice = 'r', exit_choice = 'e';


    cout << "   " << endl;
    cout << "s-search" << endl;
    cout << "i-insert" << endl;
    cout << "a-append" << endl;
    cout << "d-delete" << endl;
    cout << "r-replace" << endl;
    cout << "e-exit" << endl;
    cout << "   " << endl;
    cout << "Choose An Option: " << endl;
    cin >> choices;


    switch (choices)
    {
        case search_choice:
            cout << "Enter a word you want to search: ";
            cin >> a_string;
            char * p;
            p = strchr (a_string, 'x');
            if (p==NULL)
                cout << "not found";
            else
                cout << "found" << endl;
           // cout << "You Choose: Search";

        break;
        case insert_choice:
            cout << "Enter a string you want to insert: ";
            cin >> a_string;
            strncpy (dest, a_string,3);
            dest[3] = '\0';
            strcat (dest, a_string);
            strcat (dest, &a_string[2]);
            cout << dest << endl;
        break;
        case append_choice:
            cout << "Enter a string you want to append: ";
            cin >> a_string;
        break;
        case delete_choice:
            cout << "Enter a string you want to delete: ";
            cin >> a_string;
            strncpy (dest, a_string,2);
            dest[2] = '\0';
            strcat (dest, &a_string[8]);
            cout << dest << endl;
            break;
        case replace_choice:
            cout << "Enter a string you want to replace: ";
            cin >> a_string;
            strncpy(dest, a_string,3);
            dest[3] = '\0';
            strcat (dest, a_string);
            strcat (dest, &a_string[7]);
            cout << dest << endl;
        break;
        case exit_choice:
            cout << "You Choose: Exit";

        break;
        default: cout << "Please Enter A Valid Letter ";
        break;
    }



return 0;

}


Topic archived. No new replies allowed.