Jump to content

Runtime error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
lionaneesh

lionaneesh

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
#include<stdio.h>


void reverse(char str[]);


int main()

{

    char str[100];

    int i;


    printf("Enter to reverse : ");

    scanf("%s",str);

    reverse(str);

    return(0);

}

/* Function */


void reverse(char str[])

{


    int i;

    for(i=99;i>=0;i--)

    {

        printf("%c",str[i]);

    }

    putchar('\n');

}


Why there is a runtime error in the above programme..........

help!!!!!!
URJENT!!!!!!!!

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
OK...

Here is what I did and what you did wrong ^_^
your scanf only kept reading until it found a space...
So to change that you change your scanf to this: scanf("%[^\n]",str);
What this does is it keeps reading data until you press Enter...

Now for the next part...
Because you print out the array in reverse from 99, you are bound to print some 'garbage'... Which you most likely already discovered...
to fix that, you need to change your for loop...
from i=99;
to i = strlen(str);
So it starts at the last place where it has user data...

I hope that makes sense...
Let me know... ^_^

#3
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
What type of runtime error are you getting

#4
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
I'm guessing here...
But when running his program, it would print out garbage(because he started printing out all the characters from 99 which may contain garbage, as it is pointers...)
and secondly he didn't get a whole line like he intended to do. only one word at a time...

There were no seg faults or the like though so I'm guessing he just meant a logical screw-up...
not really a runtime error...

#5
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
 for(i=99;i>=0;i--)
    {
        printf("%c",str[i]);
    }
when i=0, you decrease it once more, so it turns out to be -1, and then you access str[-1], which is illegal.
change the loop condition to i>0.

Edited by bobdark, 23 March 2010 - 12:57 PM.
type


#6
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
yes i agree that he would get garbage for the elements

#7
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts

bobdark said:

 for(i=99;i>=0;i--)
{
printf("%c",str[i]);
}
when i=0, you decrease it once more, so it turns out to be -1, and then you access str[-1], which is illegal.
change the loop condition to i>0.

I'd hope not, the incremental or decremental expression of a for loop should be ran after the body of the for loop.
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
[Oops. Wrong thread.]

#9
lionaneesh

lionaneesh

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts

lintwurm said:

OK...

Here is what I did and what you did wrong ^_^
your scanf only kept reading until it found a space...
So to change that you change your scanf to this: scanf("%[^\n]",str);
What this does is it keeps reading data until you press Enter...

Now for the next part...
Because you print out the array in reverse from 99, you are bound to print some 'garbage'... Which you most likely already discovered...
to fix that, you need to change your for loop...
from i=99;
to i = strlen(str);
So it starts at the last place where it has user data...

I hope that makes sense...
Let me know... ^_^


Thnx a ton .....

i found ma problem........