-5

Example of what I am trying to do:

String = "This Is My Sentence"

I am looking to get this as a result: "TIMS", which takes only first letter of every word.

I am struggling with C++.

Irfan
  • 2,713
  • 3
  • 29
  • 43
user2499879
  • 673
  • 3
  • 10
  • 16

4 Answers4

0

Try using std::string and it's find function.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0
cout<<myString[0];
for(int i=0;i<(myString.size-1);i++)
        { if(myString[i]==" ")
          {
           cout<< myString[i+1];
          {
        }

I didn't checked if it compiles that way but you schould get the idea of this possible simple solution. It will only work with strings similar to your example. You should take a look at the Split Method like others already suggested.

Community
  • 1
  • 1
David_D
  • 173
  • 1
  • 2
  • 12
  • I dont need to check your solution to tell you that it will not work. Sorry for being brutally honest – MarsOne Aug 22 '13 at 09:57
  • Would you kindly tell me where my idea is wrong? Only Problem I see if the String has multiple Spaces in a row but it should work in given the example. – David_D Aug 22 '13 at 10:10
  • This will not work for the first letter itself. No need to explain further. – MarsOne Aug 22 '13 at 10:17
  • Thank you for that Hint I didn't thought about that. My intention was to show the questioner that you can use a string like an array since he seems like a beginner. I will edit the Solution. – David_D Aug 22 '13 at 10:23
0

Split it by spaces to an array, use a for loop to get each word and add the fist letter to a string.

Split

For loop

In the loop to get the fist value just do "string_word[0]"

Community
  • 1
  • 1
mont_
  • 303
  • 2
  • 12
0

Have a look at Boost Tokenizer - the code should look somehow like this (not tested):

std::string firstLetters(const std::string& str)
{
    std::string result="";
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> sep(" ");
    tokenizer tokens(str, sep);

    for (tokenizer::iterator tok_iter = tokens.begin();
        tok_iter != tokens.end(); ++tok_iter)
            {
                if (tok_iter->size>0)
                {
                    result+=(*tok_iter)[0];
                }
            } 
    return result;
}

Alternatively, you can also use Boost String Algorithms (again untested)

std::string firstLetters(std::string& str)
{
    std::string result="";
    std::vector<std::string> splitvec;
    boost::split( splitvec, str, boost::is_any_of(" "), boost::token_compress_on );

    //C++11:  for (std::string &s : splitvec)
    BOOST_FOREACH(std::string &s, splitvec)
    {
        if (s.size()>0)
        {
            result+=s[0];
        }
    }
    return result;
}

For completeness I should mention the strtok function, but this is more C than C++ ;-)

*Jost

Jost
  • 1,549
  • 12
  • 18
  • I love Boost, really do. But in this case I'd call overkill – nikolas Aug 22 '13 at 14:26
  • @nijansen only a little bit ;-)... Yes you're right, but imagine next time a solution for multiple delimiters is needed and I don't want to see a call of the `find` function for each delimiter (also think about multiple white space characters). It is simply the more powerful approach - that's why I posted it. – Jost Aug 22 '13 at 14:56