i want to print
myname
mynam
myna
myn
my
m

i have done in numbers but i can't print in string/char my code is

i want to print
myname
mynam
myna
myn
my
m

i have done in numbers but i can't print in string/char my code is

main()
{
clrscr();
int loop,count;
for( loop =   1; loop <= 5; loop = loop + 1 )
{
for( count = 1; count <= loop;count=count+1 )
printf("%d", count );

printf("\n");
}
getch();
}

my code out put is
12345
1234
123
12
1

You're very close. You do have to pay better attention to the loop range however. Below is a fixed version of your code where I kept the changes minimal. The comments should speak for itself:

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

int main(void)
{
    int loop,count;
    char* string = "myname";    // The string you want to print.

    // 'loop' represents the amount of characters to omit. instead of 1-5
    // I made this 0 - (strlen(string) - 1).
    for(loop = 0; loop < strlen(string); loop = loop + 1 )
    {
        // 'count' represents the character of the string to print.
        // This should go from 0 till strlen(string) - 'loop' as we omit
        // the last 'loop' characters.
        for(count = 0; count < strlen(string) - loop; count = count+1)
        {
            printf("%c", string[count]);
        }

        printf("\n");
    }

    getch();
}

Just a little heads-up: That string should ofcourse be declared as a "const char" and not a "char". Sorry for providing a bad practice.

Sorry for providing a bad practice.

Apologies in advance, but that's not the worst practice in your example. My vote would go to this gem:

for(loop = 0; loop < strlen(string); loop = loop + 1 )

The inner loop exhibits the same problem, which is calling strlen() in the condition of a loop. The length of the string doesn't change throughout the program, so repeatedly calling strlen() is unnecessary overhead. In fact, it doubles the time complexity of your loops. That's not a big deal here, but given how simple it is to avoid the problem, there's really no excuse not to.

You also forgot to include conio.h for the use of getch(). I didn't dock any points for a silly use of getch() because it was also in the OP's code. ;)

On a side note, I'd probably solve the problem with printf() format specifiers:

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

void reverse_text_pyramid(const char *s)
{
    for (size_t len = strlen(s); len > 0; --len)
        printf("%.*s\n", len, s);
}

int main(void)
{
    reverse_text_pyramid("myname");
}

Admittedly that's a more advanced solution, but it's still cool. :D

#include<stdio.h> 
    #include<conio.h> 
    int main() 
    { 
    char a[20]; 
    int i=0,j,k; 
    printf("Enter any string:"); 
    scanf("%s",a);
    while(a[i]!='\0')
    {
    i++;
    }
    for(j=1;j<=i;j++){
    for(k=0;k<=(i-j);k++)-------------------------------------------(1)
    {
    printf("%c",a[k]);
    }
    printf("\n");
    }
    getch();
    return 0;
    }

output:
Enter any string:myname
myname
mynam
myna
myn
my
m

And if you replace (1) with

for(k=(i-j);k>=0;k--)

then the output is....

emanym
manym
anym
nym
ym
m

very good idea pritam.basak

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.