Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Caun you please help me regarding this. I want print the out put like this,

12345
5432
234
433

Please help me ...

Advaced Thanks,


Regards,
narsimha
Posted
Comments
Tarun.K.S 26-Nov-10 12:17pm    
are you sure the last row is correct?
Sandeep Mewara 26-Nov-10 12:22pm    
What effort have you made till now?

BTW last one would be just '43' and not 433
LloydA111 26-Nov-10 17:52pm    
Use a for loop.

Hi,
You already have a C answer, this is a C++ one:
C++
#include <string>
#include <iostream>
void PrintReverseShorten(const char* str)
{
    std::string temp(str);
    while (temp.length())
    {
        std::cout << temp << std::endl;
        std::reverse(temp.begin(), temp.end());
        temp.erase(--temp.end());
    }
}
int main()
{
    PrintReverseShorten("12345");
    return 0;
}

cheers,
AR
 
Share this answer
 
Try something like this:

int main(int argc, char *argv[])
{
    char* word = "12345";
    
    int start  = 0;
    int finish = strlen(word);
    int direction = 1;
    int i = 0;
    
    while (start < finish) {
        if (direction == 1) {
            finish--;
            for (i = start ; i <= finish ; i++)
                printf("%c" , word[i]);
        }
        else {
            start++;
            for (i = finish ; i >= start ; i--)
                printf("%c" , word[i]);
        }
        direction=-direction;
        
        printf("\r");
        getchar();        
    }
    return 0;
}


I guess there are better ways to accomplish it but I have tried to keep it simple.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900