rotate down string

is it possible to rotate down C string?
exmaple when user input C++ is fun
sample output:

it will be C
............+
..............+

...............i
................s

.................f
..................u
...................n

dot express the blank space

n
any hints to do this?
Last edited on
Use a for loop and iterate through each character. Though you'll need to check for blank spaces.
1
2
3
4
5
6
7
8
9
string str = "C++ is fun";
size_t len = str.length();
for(int i = 0; i < len; i++)
{
     if(str[i] == ' ')
          continue;

     cout << str[i] << endl;
}

I haven't tested this code, but it should work.
Last edited on
what is the continue?i dont understand
its doesnot get the effect i want
Hi mike9407

If I am not wrong then n = number of spaces, right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream.h>
#include <conio.h>
// conio.h is used for clrscr() and getch()

void main()
{
clrscr();
char a[30];
int n,i,j;

cout<<"Enter a string: ";  cin.getline(a,30);
cout<<"Enter the number of spaces: ";  cin>>n; //make sure it is small value
clrscr();

for(i=0;a[i]!='\0';i++)
 { for(j=1;j<=n;j++)
    cout<<" ";
   cout<<a[i]<<endl;
 }
getch();
}


Look at the loop
1
2
3
4
5
for(i=0;a[i]!='\0';i++)
 { for(j=1;j<=n;j++)
    cout<<" ";
   cout<<a[i]<<endl;
 }

each time number of spaces are printed then at last the consecutive character of user's string is printed.


Try making a program which gives output if user enters: "C++ is fun"
C  
+  
+  
   
i  
s  
   
f  
u  
n  


Then later you'll be able to make a program like the one you wanted to:)



[EDIT]

continue; skips the current iteration and forces the loop to execute the next one.

for example if you want following output
1  
2  
3  
4  
6  
7  
8  
9  
then you should write
1
2
3
4
5
6
for(int i=0;i<=9;i++)
 { if(i==5)
   continue;
   else 
   cout<<i<<endl;
 }
Last edited on
Topic archived. No new replies allowed.