Skip to content

Instantly share code, notes, and snippets.

@shiponcs
Created February 20, 2023 11:29
Show Gist options
  • Save shiponcs/d7e470494836b5daf0e3f75c9eaf3043 to your computer and use it in GitHub Desktop.
Save shiponcs/d7e470494836b5daf0e3f75c9eaf3043 to your computer and use it in GitHub Desktop.
Write a program that reads twelve strings from fruits.txt, reverses each string, and outputs them to output/outfile.txt
#include <stdio.h>
int main()
{
FILE *fp;
FILE* fout;
int numfruits=12;
char myFruits[numfruits][256];
char myFruitsReversed[numfruits][256];
int i=0;int j=0;int count=0;
int indexEnd;
fp = fopen("fruits.txt", "r");
fout = fopen("output/outfile.txt", "w");
if (fp == NULL)
{
printf("there was an error opening fruits.txt\n");
return 1;
}
else if (fout == NULL)
{
printf("there was an error opening outfile.txt\n");
return 1;
}
else
{
while (!feof(fp))
{
fscanf(fp, "%s\n", &myFruits[i]);
i++;
}
for(i=0; i<numfruits;i++)
{
while(myFruits[i][j]!=0)
{
count++;
j++;
}
j=0;
indexEnd=count-1;
while(j < count)
{
myFruitsReversed[i][j]=myFruits[i][indexEnd];
j++;
indexEnd--;
}
fprintf(fout, "%s \n", myFruitsReversed[i]);
j=0;
count=0;
}
fclose(fp);
fclose(fout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment