Reverse the order of characters in a "text file". For example, computer becomes
retupmoc
Text file have this String "Computer"..
write on other file String "retupmoc".
Write this in C++..
#include <iostream>
#include <stdlib.h>
using namespace std;
//method to reverse the string
string reverseString(string str)
{
int size = str.size();
char revStr[1000];
int i=0;
while(str[i] != '\0' && str[i] != '\n')
{
size--;
revStr[i++] = tolower(str[size]);
}
revStr[i] = '\0';
return revStr;
}
int main(void)
{
//array declaration
char c[1000];
FILE *fptr, *fptr1;
if ((fptr = fopen("data.txt", "r")) == NULL)
exit(1);
fscanf(fptr, "%[^\n]", c);
fclose(fptr);
//function calling
string revStr = reverseString(c);
if((fptr1 = fopen("result.txt", "w")) == NULL)
exit(1);
int i=0;
//write to the file
while(revStr[i] != '\0' && revStr[i] != '\n')
{
fprintf(fptr1, "%c", revStr[i++]);
}
//close the file
fclose(fptr1);
return 0;
}
INPUT:
data.txt
OUTPUT:
result.txt
Get Answers For Free
Most questions answered within 1 hours.