:!: Im need to create a program for my requirement that will output:


Enter Any Word: ____ -(i typed Hello)
Hello
Hell
Hel
He
H

i think that i should use for and strlen but i cant make strlen work. please help

Thanyou,

Let's say you put your input in 'acBuffer[]'
:

int iCount = 0;
 
for (iCount = strlen(acBuffer); iCount => 0; iCount--)
{
    printf("%s\n", acBuffer);
    acBuffer[iCount] = '\0';
}

Or something like that.


ps. Don't forget to include string.h

Edited 5 Years Ago by Nick Evan: n/a

Let's say you put your input in 'acBuffer[]'
:

int iCount = 0;
 
for (iCount = strlen(acBuffer); iCount => 0; iCount--)
{
    printf("%s\n", acBuffer);
    acBuffer[iCount] = '\0';   <============it doesnt work
}

Or something like that.

cheers Niek

ps. Don't forget to include string.h

(i forgot to mentioned that i need this on C)!!!!!

this is my prog so far:

#include <stdio.h>
#include <string.h>
main(){
char acbuffer[50];
int icount=0;
clrsrc();
printf("Enter Any Word: ");
scanf("%s", &acbuffer);
strlen(acbuffer);
for (icount=strlen(acbuffer); icount>0; icount--)
{
 printf("%s\n", acbuffer);
 acbuffer[icount]='0'; <======= right here i think need to            decreased my word some how ..
}
getche();}

Need to decrease any word. EX. Hello then i will be Hell, Hel, He and so on until i doenst have any letters on it then the prog is terminated.

Edited 5 Years Ago by Nick Evan: n/a

by the way i need a new c language prog. do anyone know where i can find the latest turbo c language exe? pls help

read closer:

i said: acBuffer[iCount] = '\0';

you typed:

acbuffer[icount]='0';

your output was:

hello
hell0
hel00
he00
h000
0000

can you tell me what version your turbo c and where did you i can find download that cause im using version 2.1 and stdio.h doesnt work here...

here is an ex of a prog that my teacher gave to us but it doesnt work in version 2.1

#include <stdio.h>
#include <conio.h)
voidmain(){
clrsrc();
int a, i;
for (a=5; a>0; a--)
{
 for (i=a; i<5; i++)
 printf("x" );
 printf("y\n");
}
getche();}

thanks for your reply i really need this or i will fail my class..

Edited 5 Years Ago by Nick Evan: n/a

The latest version of the compiler can be downloaded from here:
http://www.borland.com/downloads/download_cbuilder.html

#include <stdio.h>
int main ()
{
   int a, i;
   for (a=5; a>0; a--)
   {
      for (i=a; i<5; i++)
      {     
          printf("x" );
      }
      printf("y\n");
    getchar () ;
}

Your prev code was full of non standard functions. Dont use functions like clrscr ( ) and dont use non standard headers like #include <conio.h>

Painkiller,
Im need to create a program for my requirement that will output:


Enter Any Word: ____ -(i typed Hello)
Hello
Hell
Hel
He
H

i think that i should use for and strlen but i cant make strlen work. please help

Thanyou, can you help me? this is my original post..

THe code which you have attempted, makes use of "scanf" for accepting string or char data which normally is not advised since it leaves the input stream dirty and which will cause behaviour which u had not expected.
Better use the technique which i have used while accepting char data and it will always work like a breeze.
Also you have used only one for loop to implement your logic which is not correct since you normally need one for controlling the number of chars to be printed and the second one to actually print characters.

Try somehting like this:

int main()
{
    int i = 0, j = 0 ;
    char buffer[BUFSIZ] = {'\0'} ;
    printf ("Enter the string you want to display: ") ;
    fgets (buffer, BUFSIZ, stdin) ;
    if (buffer[ strlen (buffer) - 1 ] == '\n')
        buffer[ strlen (buffer) - 1 ] = '\0' ;

    for ( i = strlen (buffer) ; i > 0; --i)
    {
        for ( j = 0; j < i; ++j)
        {
            putchar (buffer [j]) ;
        }
        putchar ('\n') ;
    }
   return 0 ;
}

PS:: And by the way my name is not Painkilller, its ~s.o.s~ :D

int main()
{
    int i = 0, j = 0 ;
    char buffer[BUFSIZ] = {'\0'} ;
    printf ("Enter the string you want to display: ") ;
    fgets (buffer, BUFSIZ, stdin) ;

    for ( i = strlen (buffer) ; i > 0; --i)
    {
        buffer[i] = '\0';
        printf("&s\n", buffer);
 
    }
   return 0 ;
}

works fine too, saves CPU-time :surprised


#1: Please stop bolding your messages. It isn't necessary.
#2: The code you posted (here with CODE tags, use them please) was:

#include <stdio.h>
#include <string.h>
main()
{
    char acbuffer[50];
     int icount=0;
     clrsrc();
     printf("Enter Any Word: ");
     scanf("%s", &acbuffer);
     strlen(acbuffer);
     for (icount=strlen(acbuffer); icount>0; icount--)
     {
          printf("%s\n", acbuffer);
          acbuffer[icount]='0'; 
     }
     getche();}

As niek_e said your '0' should be '\0'. That's almost all you need to fix.
There's one more thing but I'll let you figure that one out.

#3) main() is an integer function, therefore it must be specified as

int main()

true as hell, but he didn't say he wanted to keep it

Joking aside, it is actually is a OO practice to pass a constant variable to a display function and normally display functions dont manipulate the data passes to it.
Its bad bad bad..... :D

This article has been dead for over six months. Start a new discussion instead.