1

I need to make a program that prints a string with the first character on the first line, then the second two characters on the next line. i.e Hello

H

He

Hel

Hell

Hello

Here is what I have tried so far, but I only get one letter per line.

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

int main(int argc, char *argv[])
{
    for(int i = 1; i < argc; i++)
    {
      int wordLength = strlen(argv[i]);
      for(int j = 0; j < wordLength; j++)
      {
        printf("%c\n", *argv + i);
      }
    }
    return 0;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Ginormous
  • 13
  • 3

1 Answers1

0

By perform minor change on your code this is correct version to do that.

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

int main(int argc, char *argv[])
{

    for(int i = 1; i < argc; i++)
        {
        int wordLength = strlen(argv[i]);

        for(int j = 0; j < wordLength; j++)
        {
            for (int k=0;k<=j;k++){
                printf("%c", argv[i][k]);
            }
                printf("\n");
        }
        }
    return 0;
}

Compile and Run

gcc -Wall file.c -o file

./file salam hello

Output

s
sa
sal
sala
salam
h
he
hel
hell
hello
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31