1

I meant to write program which will simply delete single letters from the input given by user, let's say we've got some text like: "monkey eat banana" and we supposed to delete the letter 'a' from the text above.

The final output supposed to look like this: 'monkey et bnn'

I've got the code which works pretty much flawlessly with single strings, but I have to use getline() function to obtain some longer texts, that is why I have to declare array of string, in order to pass it's size in the second argument of getline() function, like so:

string text[256]; 
getline(text, 256); 

I would like to use getline() function without giving a size of an array, but I think it's impossible, therefore I need to stick with string array instead of a string.

The problem I've got is that I don't know how to correctly pass array of string, to use it as function's argument. Here's my code;

#include <iostream> 
#include <string> 

using namespace std; 

void deleteLetter(string &text[], char c) 
{ 
   size_t positionL = text.find(c); 
   if(positionL == string::npos) 
      cout << "I'm sorry, there is no such letter in text" << endl; 
   else 
      text.erase(positionL, positionL); 
      cout << "After your character removed: " << text << endl; 
} 

int main() 
{ 
   string str1[256]; 
   char a = 'a'; 
   cin.getline(str1, 256); 

   deleteLetter(str1, a); 
} 

I know it's elementary stuff, but still I can't figure it out on my own. Perhpahs I should reach out for your help.

5
  • 3
    Use std::vector< std::string > instead of an array; it's easier to pass to functions. Commented Jan 19, 2015 at 20:09
  • Also, don't forget to pass the capacity and the number of elements in the array when you pass an array. When you pass an array, there are no functions to determine the capacity nor the amount of items in the array. Commented Jan 19, 2015 at 20:11
  • Possible duplicate: stackoverflow.com/questions/17845238/… Commented Jan 19, 2015 at 20:13
  • 1
    cin.getline operates on C-strings (char arrays), not on std::string. I guess what you want is the standalone getline function: getline(cin, str) which operates on std::string - and then put this in a loop to read multiple lines until the end: while(getline(cin, str)) { deleteLetter(str, a); } Commented Jan 19, 2015 at 20:13
  • @ThomasMatthews As long as you don't want to modify the container, using std::string* or something like array_view<std::string> as a function parameter type might be a better choice: it does not require the user to use a specific container. Commented Jan 19, 2015 at 20:17

2 Answers 2

2

It sounds to me like you don't need an array of strings. Just to read as many characters the user types, into a string. getline should deal fine with this.

int main() 
{ 
    std::string str1; // just a string here, not an array.
    std::getline (std::cin,str1);

    deleteLetter(str1, 'a'); 
} 

Now you should change the signature of DeleteLetter to take a single string as argument.

void deleteLetter(std::string& text, char c);

How your are going to implement deleteLetter is another question. The way you have it, it will delete only the first occurence of 'a'.

Sign up to request clarification or add additional context in comments.

5 Comments

You're right, I got it working just the way you've described it. It deletes only character in the very first word that contains letter 'a', which is problematic..
Try putting the code that deletes the character into a while loop and it should work. As long as text.find returns something other than npos, erase it.
like so: while(text.find() != string::npos) {text.erase(positionL, positionL); } I did it, but it doesn't work
Almost. you have to store the result of text.find() to positionL. i.e. while( (positionL = text.find()) != string::npos) {text.erase(positionL, positionL); }
You have a typo: it should be 'a' not a passed to the deleteLetter function.
1

To read a string from console input (cin), you can use the getline() function:

std::string line;
std::getline(std::cin, line);

To remove all the occurrences of a given letter from a string, you can use the so called erase-remove idiom, with a combination of the string::erase() method and the std::remove() algorithm.
(Note that this idiom is usually showed applied to std::vector, but don't forget that a std::string can also be viewed as a "container of characters" stored in sequence, similar to vector, so this idiom can be applied to string content as well.)

To pass a std::string to functions/methods, use the usual C++ rules, i.e.:

  • If the function is observing the string (without modifying it), pass using const reference: const std::string &
  • If the function does modify the content of the string, you can pass using non-const reference: std::string &

A simple compilable code follows:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

//
// NOTE:
// Since the content of 'text' string is changed by the
// removeLetter() function, pass using non-const reference (&).
//
void removeLetter(string& text, char letter)
{
    // Use the erase-remove idiom
    text.erase(remove(text.begin(), text.end(), letter), 
               text.end());
}

int main()
{
    string line;
    getline(cin, line);
    cout << "Read string: " << line << endl;

    removeLetter(line, 'a');
    cout << "After removing: " << line << endl;
}

This is what I got with MSVC:

C:\Temp\CppTests>cl /EHsc /W4 /nologo test.cpp
test.cpp

C:\Temp\CppTests>test.exe
monkey eats banana
Read string: monkey eats banana
After removing: monkey ets bnn

It's not very clear to me from your question if you also want to pass vectors of strings around (probably in other parts of your code)...

Anyway, if you want a vector of strings (i.e. you want to store some strings in a vector container) you can simply combine these STL class templates like this:

std::vector<std::string> strings;

To pass that to functions/methods, use the usual C++ rules, i.e.:

  • If the function is observing the array of strings (without modifying it), pass using const references (const &): vector<string> &
  • If the function does modify the content of the vector, you can pass using non-const references (&): vector<string> &

2 Comments

Wow! That's impressive, I really appreciate the complexity of your answer and I'm really impressed by the amount of work you've put in this particular answer :) Thanks a lot
@PaulRatklif: You're welcome. I'm glad it helped. So you may want to mark this as answer (or whatever answer you consider to be the best one in this thread). This is the Stackoverflow's way of saying "thank you" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.