Word with reverse order

1

I'm doing an exercise where I have to do the given word with input exit in reverse order.

I start by counting the number of letters that have the word entered (commented in the code). After having counted the number of letters in the word I pass this value (number of letters) to another loop that will show the word in reverse order. For example: if the word is "test" I will have l = 5 , passing the value of L to I, the print order in loop would be subscript 5 of the array name , then the 4, then 3 ... but that is not working.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, l = 0;
char name[50];
printf("Tell me a word: \n");
scanf("%s", name);
for( i = 0; name[i] != '
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, l = 0;
char name[50];
printf("Tell me a word: \n");
scanf("%s", name);
for( i = 0; name[i] != '%pre%'; i++){  /* até aqui o código irá simplesmente 
contar o number de letras na palavra digitada*/
l++;
}
if(name[i] == '%pre%'){
printf("The number of letters is %d\n", l); /* o programa só funciona até
aqui*/
}
printf("The word is the reverse order is\n"); 

for( i = l; i != 0; i--){
printf("%d", name[i]);
}
return 0;
}
'; i++){ /* até aqui o código irá simplesmente contar o number de letras na palavra digitada*/ l++; } if(name[i] == '%pre%'){ printf("The number of letters is %d\n", l); /* o programa só funciona até aqui*/ } printf("The word is the reverse order is\n"); for( i = l; i != 0; i--){ printf("%d", name[i]); } return 0; }
    
asked by anonymous 22.12.2016 / 01:43

1 answer

2

You have two errors: It looks like you did not understand what was answered in previous question that you need to use %c to display characters; and it should consider position 0 as one of the characters to be printed, then you have to use the >= and not != operator. I use it to simplify and organize the code.

#include<stdio.h>
#include<stdlib.h>
int main() {
    char name[50];
    printf("Tell me a word: \n");
    scanf("%s", name);
    int i;
    for (i = 0; name[i] != '
#include<stdio.h>
#include<stdlib.h>
int main() {
    char name[50];
    printf("Tell me a word: \n");
    scanf("%s", name);
    int i;
    for (i = 0; name[i] != '%pre%'; i++);
    if (name[i] == '%pre%') {
        printf("The number of letters is %d\n", i);
    }
    printf("The word is the reverse order is\n");
    for (int j = i; j >= 0; j--) {
        printf("%c", name[j]);
    }
}
'; i++); if (name[i] == '%pre%') { printf("The number of letters is %d\n", i); } printf("The word is the reverse order is\n"); for (int j = i; j >= 0; j--) { printf("%c", name[j]); } }

See working on ideone and on CodingGround .

    
22.12.2016 / 01:51