#include<iostream>
#include<algorithm>
#include<cstring>
#define MAX 50
usingnamespace std;
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()
cout << "String: " //<< i dont know what to put here, so that the letters i inputted
//will be added to the string. help pls.
}
void remove(char * cstring, char c)
{
char * end = cstring + strlen(cstring);
end = std::remove(cstring, end, c);
*end = '\0';
}
An example would be.
Enter String: superfragilisticexpialidocious
Size of String: 30
Delete Element: a
String: superfrgilisticexpilidocious
Size of String: 28
Add Element: zzz
String: superfrgilisticexpilidociouszzz <<< This is the part i don't know how to do. please help me!
Size of String: 31
i would be very grateful. a simple code will do.
EDIT: Just realised you seem to be using cstrings (despite declaring a std::string). You'll need to use the strcat function for those. I'd recommend using C++ strings, though.
You can add a character to the string only if the character array has enough space to accomodate the character. So before adding a character you should check the current size of the array with its maximumsize.
So function add can look for example the following way.
1 2 3 4 5 6 7 8 9 10 11 12
char * add( char *s, char c )
{
int n = strlen( s );
if ( n < MAX - 1 )
{
s[n] = c;
s[n + 1] = '\0';
}
return ( s );
}
You've defined letter as a char, which means that it only holds a single character.
And I can only echo what several other people have said: you'll find all this much easier if you learn how to use std::string, instead of C-style char arrays.