-1

I'm recently practicing looping. I learned how to print: for example home to h ho hom home. by using

#include <stdio.h>
#include <string.h>

int main (){
    char s[100];
    
    printf("Input string = ");
    scanf("%[^\n]", s);
    
    for (int i=1; i<=strlen(s); i++){
        for(int j = 0; j<i; j++)
        printf("%c", s[j]);
        printf("\n");
    }

    return 0;

How can i reverse it so it can be home hom ho h instead? thank you.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
atuy
  • 13
  • 2
  • Don't use scanf: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html. In this case, you should just do `char *s = argv[1]`. If you're going to use `scanf`, there are many issues to watch out for, but at the very least protect yourself by doing: `if( scanf("%99[^\n]", s) != 1 ){ exit(1);}` – William Pursell Jan 18 '22 at 15:57
  • `scanf("%[^\n]", s);` is worse than [`gets()`](https://stackoverflow.com/q/1694036/2410359). Do not use either. – chux - Reinstate Monica Jan 18 '22 at 16:43

3 Answers3

2

It is easy to do. For example

for ( size_t i = strlen( s ); i != 0; i-- )
{
    for ( size_t j = 0; j < i; j++ )
    { 
        putchar( s[j] );
    }
    putchar( '\n' );
}

Another way is the following

for ( size_t i = strlen( s ); i != 0; i-- )
{
    printf( ".*s\n", ( int )i, s );
}

provided that an object of the type int is able to store the length of the passed string.

Here is a demonstration program.

#include <stdio.h>
#include <string.h>

int main( void )
{
    const char *s = "home";

    for (size_t i = strlen( s ); i != 0; i--)
    {
        printf( "%.*s\n", ( int )i, s );
    }
}

The program output is

home
hom
ho
h
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you so much for your detailed explanation, it helped me in my way of learning, i didn't think of that way because i'm a newbie. if you mind if i ask. what should i do if i want it to be `home` `ome` `me` `e`? Thank you. – atuy Jan 18 '22 at 16:08
  • @atuy It is easy. for ( size_t i = 0, n = strlen( s ); i < n; i++ ) puts( s + i ); – Vlad from Moscow Jan 18 '22 at 16:11
1

You could loop over the string using putc, but it might also be helpful to understand the destructive approach that shortens the string and uses %s to print strings. eg:

#include <stdio.h>
#include <string.h>

int
main(int argc, char **argv)
{
    char *s = argc > 1 ? argv[1] : strdup("home");
    for( char *e = s + strlen(s); e > s; e -= 1 ){
        *e = '\0';
        printf("%s\n", s);
    }
    return 0;
}

Note that this approach is destructive. When complete, the string is null. As an exercise, it might be helpful to fix that.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

You'll basically go backwards in your loop.

Instead of:

    for (int i=1; i<=strlen(s); i++){

You'd have

    for (int i=strlen(s); i>0; i--){
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • @atuy try to learn :) which loop controls this behavior? Basically you need to figure out where to start your index count in that loop and which way to move through it. You have ```i``` and ```j``` and ```++``` and ```--``` - figure out which one of each it should be – littleadv Jan 18 '22 at 16:11