TurtleMan TurtleMan - 1 month ago 6
C Question

Trouble with reversing an array while reading input from a file in C

I am currently trying to work on a program that will read in a file line by line and place the content in an array. Then trying to reverse the original array into a second array so I can later compare the two arrays. My current code is

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

void reversing(char buffer[], char reverse[], int stringLength);

int main(int argc, char const *argv[])
{
int stringLength = 0;
FILE *fp; //Declaring a FILE type pointer calling it fp
char buffer[64]; //Declaring a char array the size of 64 named buffer

fp = fopen("./test.txt", "r");
if(fp == NULL)
{
printf("File did not open!\n");
}
else
{
while(fgets(buffer, 64, (FILE*)fp) != NULL)
{
printf("\nNormal String: %s", buffer); //Print content in buffer(original array)
stringLength = strlen(buffer); //Storing the length of the readed input

char reverse[stringLength]; //Creating array for reversed comparision(comparision not implemented yet)

printf("String length: %d\n", stringLength); //Print string length(for debugging)
reversing(buffer, reverse, stringLength);
printf("Reversed String: %s\n", reverse);//Print content in reverse(Reverse array)
}

printf("\n\nEND OF FILE Reached!\n");

fclose(fp);
}
return 0;
}

void reversing(char buffer[], char reverse[], int stringLength)
{
int i = 0;
int j = stringLength - 1;

while(i < j)
{
reverse[j] = buffer[i];
i++;
j--;
}
}


My test file is simply

A
AB
ABC
ABCD
ABCDE


It should print to the screen

Normal String: A
Reversed String: A
Normal String: AB
Reversed String: BA
Normal String: ABC
Reversed String: CBA
Normal String: ABCD
Reversed String: DCBA
Normal String: ABCDE
Reversed String: EDCBA


When I run the program through a debugger it shows that once inside the reversing function the letters are being swap correctly; however, once it exits the function and the call to print the reversed array is made it is printing garbage, but inside the garbage are the first two elements swap correctly. Any help of suggestion would be greatly appreciated.

As a side note
I am trying to make this as simple as possible since the true objective or this program is to take it and implement it in assembly on a ci20 machine.

Answer

Your problem lies in the line while (i < j) in your reversing function. i will end up being less than j when only half of your array will be reversed. Instead, it should be while (i < stringLength)

You should also change your character array char reverse[stringLength] to char reverse[stringLength + 1] so you have space for the '\0' terminating character, which you should add to the array at the end of your reversing function.

It should look like this:

void reversing(char buffer[], char reverse[], int stringLength)
{
    int i = 0;
    int j = stringLength - 1;

    while(i < stringLength)
    {
        reverse[j] = buffer[i];
        i++;
        j--;
    }
    reverse[i] = '\0';
}

Also note that the read function for the file is also reading in a newline character and the reversing function is placing it at the beginning of your reversed string, so output looks like this:

Normal String: A
String length: 2
Reversed String: 
A

Normal String: AB
String length: 3
Reversed String: 
BA

Normal String: ABC
String length: 4
Reversed String: 
CBA

Normal String: ABCD
String length: 5
Reversed String: 
DCBA

Normal String: ABCDE
String length: 6
Reversed String: 
EDCBA


END OF FILE Reached!

I'll leave it up to you to figure out removing the newline character before sending it to the reversing function. If you can't figure it out, just comment it under this answer and I'll edit the answer.