Am I even doing this right?

I am still new how would I put in a function to replace certain characters with others and did I even compress this string right? Here is what I have to do.

Write two general functions to process strings.

The first function is named Compress. It takes two parameters. The first is the string to be compressed. The second parameter is the character that is to be removed from the string, for instance a space (but it would not need to be a space: it could be any character)
The second function is named Replace. It takes three parameters. The first is the string to be considered. The second is the character to be found in the string. The third is the character to replace the found character with.

This is what I have so far...

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void compress(string, string&);
string compress(string);
string remove(string, char);

void main( ){
string a, b, c, d;

getline(cin, a);

compress(a, b);
cout << "Compress Process Function: " << b << endl;

c = compress(s);
cout << "Compress Value Function : " << c << endl;

d = remove(a, 't');
cout << "Remove all lower 't' : " << d << endl;

d = remove(a, 's');
cout << "Remove all lower 's' : " << d << endl;
}

void compress(string a, string& b){
string answer;
int i;

answer="";
for (i=0; i< s1.length(); i++){
if (s1.at(i)!=' ')
answer = answer + s1.at(i);
}
b = answer;
}
string compress(string s1){
string answer;
int i;

answer="";
for (i=0; i< s1.length(); i++){
if (s1.at(i)!=' ')
answer = answer + s1.at(i);
}
return(answer);
}
string remove(string a, char ch){
string answer;
int i;

answer="";
for (i=0; i< a.length(); i++){
if (a.at(i)!=ch)
answer = answer + a.at(i);
}
return(answer);
}
The compress functions and remove are basically doing the same. I'd say that you omit the compress functions.

You haven't implemented the replace function. You can implement it similar to remove where the if has an else for the char to replace
Topic archived. No new replies allowed.