Oct 2, 2015 at 9:13am UTC
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 Oct 2, 2015 at 9:37am UTC
Oct 2, 2015 at 9:54am UTC
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 Oct 2, 2015 at 10:00am UTC
Oct 2, 2015 at 10:57am UTC
what is the continue?i dont understand
Oct 2, 2015 at 12:14pm UTC
its doesnot get the effect i want
Oct 2, 2015 at 3:30pm UTC
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"
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
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 Oct 2, 2015 at 3:47pm UTC