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

void reverse(char str[]);

int main()
{
    char str[100];
    int i;

    printf("Enter to reverse : ");
    scanf("%[^\n]",str);
    reverse(str);
    return(0);

}
/* Function */

void reverse(char str[])
{

    int i;
    for(i=(strlen(str));i > 0; i--)
    {
        printf("%c",str[i]);
    }
    putchar('\n');
}

in this programme when we input 123
output = 32 /* non - desired result 1 missing */

input = sonal , output = lano /* s missing*/

input = aneesh ,output = hseen /*a missing */

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

void reverse(char str[]);

int main()
{
    char str[100];
    int i;

    printf("Enter to reverse : ");
    scanf("%[^\n]",str);
    reverse(str);
    return(0);

}
/* Function */

void reverse(char str[])
{

    int i;
    for(i=(strlen(str));i >=0; i--)
    {
        printf("%c",str[i]);
    }
    putchar('\n');
}

in this program wen we input 123 the output is 231...
and:-
input = aneesh ,output = hseen
but by my understanding this for loop is wrong..


NOW the questions:-
1. Why in the first program the output in the case "123" is "32" and in case "aneesh" output is "hseen" ?

2. Why the second program provides right results despite the loop is wrong(by my understanding)?

Recommended Answers

All 4 Replies

In your second example you haven't noticed the additional space that's being put in front of the word. This could very well be any junk character because when you reference str[strlen(str)] you overstep the bounds of the array. Remember, if your string is length 3 you're only going to have characters 0,1,2. So change that on line 23 to i = strlen(str) -1 and you'll have it right. You already figured out how you were missing the first element of the string when you had > instead of >=.

I would compliment you on a well written post (among all your other ones) but the title still needs work.

In your second example you haven't noticed the additional space that's being put in front of the word. This could very well be any junk character because when you reference str[strlen(str)] you overstep the bounds of the array. Remember, if your string is length 3 you're only going to have characters 0,1,2. So change that on line 23 to i = strlen(str) -1 and you'll have it right. You already figured out how you were missing the first element of the string when you had > instead of >=.

I would compliment you on a well written post (among all your other ones) but the title still needs work.

Thank u Sir and i will improve my question next time thanks...

In your second example you haven't noticed the additional space that's being put in front of the word. This could very well be any junk character because when you reference str[strlen(str)] you overstep the bounds of the array. Remember, if your string is length 3 you're only going to have characters 0,1,2. So change that on line 23 to i = strlen(str) -1 and you'll have it right. You already figured out how you were missing the first element of the string when you had > instead of >=.

I would compliment you on a well written post (among all your other ones) but the title still needs work.

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

void reverse(char str[]);

int main()
{
	char str[100];
	int i;

	printf("Enter to reverse : ");
	scanf("%[^\n]",str);
	reverse(str);
	return(0);

}
/* Function */

void reverse(char str[])
{

	int i;
	for(i=strlen(str)-1;i > 0; i--)
	{
		printf("%c",str[i]);
	}
	putchar('\n');
}

this again doesn't work....

for(i=strlen(str)-1;i >= 0; i--) You needed both the -1 and the >=0. I had said you were on the right track with the >= sign, otherwise you lose the first character in the string.

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.