2

Suppose I have a string s which is given below:

string s="i am\ngoing\nto\ncuet";

I want to align the string to the right during display in console. So I want to show output like this:

EDIT: rightmost characters should be aligned.

                                      i am
                                     going
                                        to
                                      cuet

I tried this code to show the output:

cout.width(75);
cout<<s;

But it only right align the first line like this:

                                                         i am
going
to
cuet

Then I tried this code to get the output:

for(int i=0 ; i<s.size(); i++)
    {
        cout.width(75);
        cout<<s[i];
    }

But I get peculiar output using this code:

                                                                  i

                                                        a
                                                   m

                                                                  g
                                                             o
                                                        i
                                                   n
                                              g

                                                                  t
                                                             o

                                                                  c
                                                             u
                                                        e
                                                   t

How can I get the desired output?

akid
  • 23
  • 2
  • 6
  • This is not text aligned to the right, and you are not describing text aligned to the right. Text aligned to the right have its **right**most characters **aligned**. – n. m. could be an AI Mar 26 '17 at 20:03
  • You need to handle **lines**, thus your code needs to be aware of strings that contain multiple lines, and explicitly process each line separately. It isn't, and doesn't. – n. m. could be an AI Mar 26 '17 at 20:08
  • @n.m. how can i explicitly process each line? can you give an example? – akid Mar 26 '17 at 20:14
  • helpful reading: http://en.cppreference.com/w/cpp/io/manip/left, http://en.cppreference.com/w/cpp/string/basic_string/getline, and http://en.cppreference.com/w/cpp/io/basic_stringstream – user4581301 Mar 26 '17 at 20:47

3 Answers3

4

You need to read s line by line, then output each line right aligned.

#include <iostream>
#include <iomanip>
#include <sstream>

void printRightAlignedLines(const std::string& s, int width)
{
    std::istringstream iss(s); //Create an input string stream from s
    for (std::string line; std::getline(iss, line); ) //then use it like cin
        std::cout << std::setw(width) << line << '\n';
}

int main()
{
    std::string s = "i am\ngoing\nto\ncuet";
    printRightAlignedLines(s, 75);
}
tnt
  • 1,174
  • 2
  • 10
  • 14
1

Easy Solution with queue:

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
str = "i am\ngoing\nto\ncuet";
queue <char> q;
int i;
int len=str.size();
for(i=0; i<len; i++)
{
if(i==len-1)q.push(str[i]);

    if(str[i]=='\n' || i==len-1)
    {
        cout.width(75);
        while(!q.empty())
        {
            cout<<q.front();
            q.pop();
        }
        cout<<"\n";
    }
    else
        q.push(str[i]);
}
return 0;
}
0
#include<bits/stdc++.h>
using namespace std;

int main(){

    string s="i am\ngoing\nto\ncuet";
    string p = "";

    for(int i=0;s[i]!='\0';i++){

        p = p+s[i];

        if(s[i]=='\n'){
            cout.width(75);
            cout<<p;
            p = "";
            continue;
        }

        if(s[i+1]=='\0'){
            p = p+'\n';
            cout.width(75);
            cout<<p;
            p = "";
        }


    }


}
Nafis Islam
  • 41
  • 1
  • 7