So I have a task which requires me to manipulate arrays and reverse a string. I have written the code but it doesn't work quite perfectly. The problem is that it reverses the string BUT it also includes weird symbols after the reversed string. I'm pretty new to programming and I have tried to find an answer elsewhere but don't know what to look for exactly. I appreciate any constructive feedback, Thanks!
#include <stdio.h>
int main(){
char string[256];
char output[256];
int begin;int end;
int count = 0;
printf("Input a string\n");
fgets(string, 256, stdin);
while (string[count] != '\0'){
count++;
end = count - 1;
}
for (begin = 0; begin < count; begin++) {
output[begin] = string[end];
end--;
}
string[begin] = '\0';
printf("%s\n", output);
}Videos
What is the best way to reverse a string in C?
Can I reverse a string with the for loop in C?
Can I reverse a string without using a loop?
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
The function gets is unsafe and is not supported by the C Standard. Instead use either scanf or fgets.
The function strlen is a standard C string function. So according to the requirement you may not use it.
You are not reversing a string. You are trying to copy a string in the reverse order into another string.
The program can look the following way
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "", rev[N];
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
rev[n] = '\0';
for ( size_t i = 0; i < n; i++ )
{
rev[n - i - 1] = in[i];
}
puts( rev );
}
If you actually need to reverse a string in place then the program can look the following way
#include <stdio.h>
int main(void)
{
enum { N = 15 };
char in[N] = "";
printf("Enter a word (upto %d letters): ", N - 1 );
scanf( " %14s", in );
size_t n = 0;
while ( in[n] ) ++n;
for ( size_t i = 0; i < n / 2; i++ )
{
char c = in[i];
in[i] = in[n - i - 1];
in[n - i - 1] = c;
}
puts( in );
}
EDIT: getline is not standard C, and it is only recognized by POSIX systems. Another solution is to use fgets that works for both OSes. I provided both examples.
As others have already pointed out, you are making some mistakes:
- Unsafe practice when getting input from the user.
- Always starting from 15 even if the input string has less chars.
I have created a little example with dynamic allocation that works with more than 15 characters and fixes the afore-mentioned issues. Comments inline to key points.
Example: getline - POSIX
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
// Idea from https://stackoverflow.com/questions/7709452/how-to-read-string-from-keyboard-using-c
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
read = getline(&line, &len, stdin);
if (read > 0)
{
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
// "read-1" because we start counting from 0.
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free (line); /* free memory allocated by getline */
free(rev_line);
return 0;
}
Example: fgets - C standard
fgets does not return the number of characters read, so it has to be chained with strlen to decide how many characters to allocate for the reversed string.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int main (int argc, char *argv[]) {
char line[LINE_MAX];
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
if (fgets(line, LINE_MAX, stdin) != NULL)
{
line[strcspn(line, "\n")] = '\0'; //fgets() reads the \n character (that's when you press Enter).
read = strlen(line);
printf ("\n String from user: %s\n", line);
}else
{
printf ("Nothing read.. \n");
return -1;
}
// Now we need the same amount of byte to hold the reversed string
char* rev_line = (char*)malloc(read);
for (int i = 0, j = read-1; i < read; i++, j--)
{
rev_line[i] = line[j];
}
printf("%s\n",rev_line);
free(rev_line);
return 0;
}