Print string vertically

The complete question is : 12. Write a function printVertically that takes a parameter of type string and displays the string of characters vertically. The function does not return anything to main. Write main to test this function. Use of array is not allowed.

My doubt is : What approach can be used to display characters in a string vertically without using array? Remaining Part of question I have understood and will do myself.
std::string is essentially an array. See
http://www.cplusplus.com/reference/string/string/operator[]/
If i dont want to use str.length(), what else can indicate end of string ?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;
int main ()
{
  string str ("Test string");
  for (int i=0; i<str.length(); ++i)
  {
    cout << str[i]<<endl;
  }
  return 0;
}
Last edited on
closed account (48T7M4Gy)
tellg

http://www.cplusplus.com/forum/beginner/220908/#msg1015406
closed account (48T7M4Gy)
Even simpler, don't need tellg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    
    
    std::string input;
    std::cout << "Please enter a string: ";
    std::cin >> input;
    
    std::istringstream iss;
    iss.str (input);
    
    char ch;
    
    while(iss >> ch)
    {
        std::cout << ch << '\n';
    }
    
    return 0;
}
1
2
3
4
#include <string>

// Write a function printVertically that takes a parameter of type string and displays the string of characters vertically.
void printVertically( const std::string& str ) { for( char c : str ) std::cout << c << '\n' ; }

closed account (48T7M4Gy)
Another variation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
    std::string input;
    std::cout << "Please enter a string: ";
    std::cin >> input;
    
    char ch;
    int index = 0;
    
    while((ch = input[index]))
    {
        std::cout << ch << '\n';
        index++;
    }
    
    return 0;
}
I'm always a bit befuddled by responses like "I don't want to use <API>" when assignment says "I must use library object with <API>".

You are asked to use a std::string, not an array. std::string.length() is part of std::string's public interface; you have absolutely no reason not to.

Here's the kicker: a std::string is a fancy thing that manages an array of characters for you. So you can index it just like a normal array. Conveniently, it also provides you with methods to track how long the array is.

Compare:

1
2
3
4
5
void display_vertically( const char* s, int length )
{
  for (int n = 0; n < length; n++)
    ...
}
1
2
3
4
5
void display_vertically( std::string s )
{
  for (int n = 0; n < s.length(); n++)
    ...
}

Life is easy.

What the first example leaves out is all the effort you had to go to in main() to create and populate and free the char array. The second example relieves you of that.

1
2
3
4
5
6
7
8
int main()
{
  cout << "Hey, enter a string: ";
  string s;
  getline( cin, s );

  display_vertically( s );
}

Sweet!

tl;dr: use the tools given to you; don't look for reasons not to use them.
Last edited on
Thanks keskiverto, kemort, Borges, Duthomas. Thanks a lot everyone.

Duthomas, actually I didn't want to use str.length() as it was not covered in class yet. That's why I thought of asking some alternatives.
str.length() as it was not covered in class yet.

Ok,
1. Some part of string API has been covered.
2. You have to use string.

The obvious followup question is: What bits of string API has been covered?

Depending on that answer there are multiple scenarios. For example,
* The already covered API is sufficient.
* The covered API is not enough, but student is expected to read ahead.
* The API was covered, but forgotten.
* ...
> I didn't want to use str.length() as it was not covered in class yet.
> That's why I thought of asking some alternatives.

The canonical way to iterate through an entire sequence is to use a range based loop.
http://www.stroustrup.com/C++11FAQ.html#for

Even after it is covered in class; strongly favour using a a range based loop for simple loops like this. Resort to the classical for loop only when there is a more complex requirement.
closed account (48T7M4Gy)
@shivamjain1

So there you go, we have numerous alternatives arising from your original question. I for one have leaned quite a bit. Well done! :)
JLBorges +1

1
2
  for (auto c : s)
    ...
Topic archived. No new replies allowed.