Alright, I'm having another problem in C. I want to make a program that takes a string inputted by the user and reverses it. I got it to work, but it displays gibberish at the start, which I can't get rid of.
Here is the function prototype:
Here is the function definition:
and here is the source:
I defined a variable to 700, because that will accommodate most strings, since I have no idea how long the actual string a user inputs will be exactly. Both putchar and printf give me gibberish.
Here is the function prototype:
Code:
void reverse(char string[], int size);
Here is the function definition:
Code:
#include <stdio.h>
void reverse(char string[], int size)
{
int count = size -1;
for (count; count >= 0; count--)
{
if (string[count] >= 33 && string[count] < 123| string[count] >= 65 && string[count] < 123 | string[count] >= 97 && string[count] < 123)
{
printf("%c", string[count]);
}
}
printf("\n");
}
and here is the source:
Code:
#include <stdio.h>
#define Size 700
int main()
{
char sentence[Size];
int items = Size;
printf("Enter a string: ");
fgets(sentence, Size, stdin);
reverse(sentence, items);
return 0;
}
I defined a variable to 700, because that will accommodate most strings, since I have no idea how long the actual string a user inputs will be exactly. Both putchar and printf give me gibberish.