Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

How to reverse strings effeciently

I have a program that reverses the contents of a file byte by byte, but that is rather inefficient and I would like to take larger chunks of bytes and reverse those; still keeping the order (reversed) but doing it faster and with less iterations.  Here is the byte by byte code. The file to read is given as the first comand line argument.

#include <fcntl.h>
const char lf = '\n';

int main(int argc, char* argv[]) {
  int  inputFD;
  char inputByte;
  long currentLoc;

  inputFD = open(*++argv, O_RDONLY);
  currentLoc = lseek(inputFD, -1L, 2);
  while (currentLoc >= 0) {
    read(inputFD, &inputByte, 1);
    write(1, &inputByte, 1);
    currentLoc = lseek(inputFD, -2L, 1);
  }
  write(1, &lf, 1);
  close(inputFD);
}

Thanks for any suggestions.

-Shiromagius
0
Shiromagius
Asked:
Shiromagius
1 Solution
 
sunnycoderCommented:
1. read in a chunk
2. reverse it in a buffer (use strrev() or any other function that suits your data )
3. write it

e.g. lseek to 500 bytes from end
read 500 bytes into buffer
print the buffer in reverse
next lseek to 500*number of iterations

return value of lseek can be used for terminating the loop
0
 
ShiromagiusAuthor Commented:
Are there any string reversing functions in the standard C libraries? I cant seem to find strrev().
0
 
chikucoderCommented:
>>Are there any string reversing functions in the standard C libraries? I cant seem to find strrev().

Nope
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AjarCommented:
I suggest  following
read the whole file  in a single go and then   reverse the whole string

SAMPLE program ..

#include <stdio.h>
#include <sys/stat.h>

int main(int argc, char * argv[])
{
FILE * f ;
char * c ;
long   len;
struct stat sb;
if(argc < 2) return 0;
//FIND FILE STATASTICS ...
if(stat(argv[1],&sb) {printf("FILE NOT EXISTANT %s \n",&argv[1]);return 0;}
// ALLOCATE MEMORY EQUAL TO FILE SIZE
c = (char* ) malloc(sb.st_size);
//READ FILE
f = fopen(argv[1],"r");
len = fread(c,1,sb.st_size,f);
fclose(f);
//Print the reverse of string
{
long k = len
while(k) putchar ( c[k--]);
}
// REverse the string in memory
{
  long k = len/2;
  while(k) {c[k]^=c[len-k]^=c[k]^=c[len-k];k--}
}
//DO PROCESSING WITH c

free(c);
return 0;
}
0
 
sunnycoderCommented:
strrev is not included in ANSI standard and is available on some platforms
0
 
ShiromagiusAuthor Commented:
Thanks everyone. I got it all worked out. Thank you everyone for their comments.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

I know that this has been closed, but you CAN reverse the entire file easily and reasonably efficiently.  Use the mmap() function to map the entire file to paged memory and then just print the array backwards.  Here's a shell of a program to do that:

#include <stdlib.h>
#include <sys/mman.h>
#include <sys/type.h>
#include <sys/stat.h>
#include <unistd.h>

int     FileHandle;
char *FileName = "TestFile";  // Hard coded for simplicity
char *FileBuffer;

FILE *Output;
char *OutputName = "NewFile";

struct stat  Stat;

main ()
{
    int idx;

/*  open input file  */

    FileHandle = open (FileName, O_RDONLY);  // Test for errors
    fstat (FileHandle, &Stat);
    FileBuffer = mmap (NULL, Stat.st_size, PROT_READ, MAP_SHARED, FileHandle, 0);

/*  open output file  */

   Output = fopen (OutputName, "w");

/*  copy file  */

    for (idx = Stat.st_size; idx; idx--)
      putc (FileBuffer+idx, Output);

    fclose (Output);
    munmap (FileBuffer, Stat.st_size);
    close (FileHandle);
}



Kent
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now