c string program

my assignment is : Please use C type strings ( array representation of strings).

Write a program that will ask the user to enter a string. It will then regard that string as a worked-on

string and allow the user to perform the following editing functions on it:

s – search

i – insert

a – append

d – delete

a – append

d – delete

r – replace

e – exit

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.

here is what i have so far.



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

using namespace std;

int main()
{

    char a_string[80];
    cout << " Enter A String:\n";
    cin.getline(a_string,80);
    cout << "Current String: " << a_string << endl;
    
    return 0;
}


Are you "allowed" to use the functions contained in <cstring>? Everything you need is within that header.
yeah i am supposed to use <cstring>

i need help implementing the options for the user to enter to work on the string
You need a while loop and a switch statement:
http://www.cplusplus.com/doc/tutorial/control/#while
http://www.cplusplus.com/doc/tutorial/control/#switch

And to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
bool running = true;
while ( running )
{
  char choice;
  std::cin >> choice;  std::cin.ignore( 80, '\n' );

  switch( choice )
  {
  case 'e': running = false; break;
  default:
  }
}
Last edited on
how do i implement this with what i currently have ? i am still having a hard time with this assignment..

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

#include <iostream>
#include <cstring>

using namespace std;

int main()
{

    char a_string[80];

    cout << "Enter A String:\n";
    cin.getline(a_string,80);
    cout << "  " << endl;
    cout << "Current String: " << a_string << endl;
    cout << "  " << endl;
    cout << "Choose An Option: " << 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;
    bool running = true;
while ( running )
{
  char choice;
  std::cin >> choice;  std::cin.ignore( 80, '\n' );

  switch( choice )
  {
  case 'e': running = false; break;
  default:
  }
}


}


Last edited on
Topic archived. No new replies allowed.