Erdowi Erdowi - 9 days ago 6
C Question

C Help - output to spell input by letters

I'm trying to figure out the best way to have C spell out a given word. I already have it spelling the given word in reverse.

Input: test

Output:

t

e

s

t

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

void reverse(char [], int, int);
int main()
{
char str1[12];
int size, counter;

printf("Enter a string to reverse: ");
scanf("%s", str1);
printf("string1 with each character on a new line:")


printf("string1 printed backwards: \n");
size = strlen(str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
return 0;
}

void reverse(char str1[], int index, int size)
{ // Reversing the string using recursion
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}

Answer
printf("Enter a string to reverse: ");
scanf("%s", str1);
size = strlen(str1);
printf("string1 with each character on a new line:\n");
int i;
for(i = 0; i < size; i++)
    printf("%c\n", str1[i]);

printf("string1 printed backwards: \n");