Elements

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
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAX 50
using namespace std;
int main()
{
    void remove(char[], char);
    char cstring[MAX];
    char letter;
    std::string str;

    cout << "Enter String: ";
    cin.getline(cstring, MAX);

    cout << "Size of String: " << strlen(cstring) << endl;

    cout << "Delete Element: ";
    letter = cin.get();

    remove(cstring, letter);

    cout << "String: " << cstring << endl;
    cout << "Size of String: " << strlen(cstring) << endl;

    cin.ignore();

    cout << "Add Element: ";
    letter = cin.get();

    add(cstring, letter);

    cout << "String: " << cstring << endl;


}

void remove(char * cstring, char c)
{
    char * end = cstring + strlen(cstring);
    end = std::remove(cstring, end, c);
    *end = '\0';
}
    char * add( char *cstring, char c )
{
   int letter = strlen( cstring );

   if ( letter < MAX - 1 )
   {
      cstring[letter] = c;
      cstring[letter + 1] = '\0';
   }

   return ( cstring );
}


the char * add part does not seem to work. Can you please modify it? and post the code. It'll be a great help. Thank you!
Last edited on
http://www.cplusplus.com/forum/beginner/105131/

Please don't double post. It makes it more difficult for people answering to track responses and for future viewers to find resolutions.
i'd use std::string to be honest.
from http://www.cplusplus.com/reference/string/string/
operator+= - Append to string (public member function )
append - Append to string (public member function )
push_back - Append character to string (public member function )
Topic archived. No new replies allowed.