Advertisement
Eksekk

Initials

May 20th, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. std::string initials(const std::string& str)
  5. {
  6.     std::string in = "";
  7.     in += str[0];
  8.     for (int i = 0; i < str.size(); ++i)
  9.     {
  10.         if (str[i] == ' ' && i + 1 < str.size() && str[i + 1] != ' ')
  11.         {
  12.             in += str[i + 1];
  13.         }
  14.     }
  15.     return in;
  16. }
  17.  
  18. int main()
  19. {
  20.     std::cout << initials("John Fitzgerald Kennedy") << std::endl;
  21.     std::cout << initials(std::string("andy warhol")) << std::endl;
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement