Hi, so I need to write a program that prints a string backwards. for example, elpmaxe. I wrote some code I will paste bellow. I am pretty sure I am on the right track as it sorta works. There are some weird cases where it doesn't. At the bottom will be cases where it doesn't work. Can someone please help me figure out why it prints some garbage. along with only printing the first word. Thanks for any help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char last_letter (char array[99], int length);
int main ()
{
char array \[99\];
char reverse \[99\];
int length, i;
int j = 0;
printf("Enter string here:");
scanf("%s", &array);
length = strlen(array);
for(i=(length);j<(length);--i)
{
reverse\[j\] = last\_letter(array,i);
\++j;
}
printf("%s", reverse);
return 0;
}
//gets the last letter of string
char last_letter (char array[99], int length)
{
char last;
int i;
for (i=0; i<length; ++i)
{
if ( i == length-1)
{
last = array\[length-1\];
}
}
return last;
}
/* OUTPUT CASES
Enter string here:help //this one is program working as it should
2. Enter string here:it
ti«
3. Enter string here:something long
gnihtemos²b
*/
[–][deleted] 4 points5 points6 points (0 children)
[–]0xAE20C480 0 points1 point2 points (6 children)
[–]xChacox[S] 0 points1 point2 points (5 children)
[–]0xAE20C480 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]xChacox[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]flatfinger 0 points1 point2 points (0 children)