1

i'm trying to create a map of word==>drow, like polindrom... the problem is at the final level at "strtok"... first i split it, then in subsequent call when doing strtok(NULL," "); it works ok. the problem is when i add the second string "poly_buffer"... seems it works on it....

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <string>
using namespace std;

void poly(char *buffer)
{
 char temp;
 for (int i=0; i<=strlen(buffer); i++)
 {
  int word_start = i, word_stop = i;

  while (buffer[i] != 32 && buffer[i] != '\0') { i++; word_stop++; }
  word_stop--;

  //swap chars until the middle of word
  while (word_stop >= word_start)
  {
   //swap the chars
   temp = buffer[word_stop];
   buffer[word_stop] = buffer[word_start];
   buffer[word_start] = temp;
   word_stop--;
   word_start++;
  }
  word_start = i;

 }
}


void main()
{
 FILE *fp;
 char *buffer;
 char *poly_buffer;
 long file_size;
 map<string,string> map_poly;

 fp = fopen("input.txt", "r");

 if (fp == NULL) { fputs("File Error",stderr); exit(1); }

 //get file size
 fseek(fp,1,SEEK_END);
 file_size = ftell(fp);
 rewind(fp);

 //allocate memory
 buffer = new char[file_size+1];
 poly_buffer = new char[file_size+1];

 //get file content into buffer
 fread(buffer,1, file_size,fp);
 strcpy(poly_buffer,buffer);

 buffer[file_size] = '\0';
 poly_buffer[file_size] = '\0';

 poly(buffer);

 buffer = strtok(buffer," ");
 poly_buffer = strtok(poly_buffer," ");

 while (buffer != NULL)
 {
  map_poly[buffer] = poly_buffer;
  printf("%s ==> %s\n", buffer, poly_buffer);
  buffer = strtok(NULL," ");
  poly_buffer = strtok(NULL," ");
 }

 fclose(fp);
 while(1);
}

what am i doing wrong ?

2
  • The word you're looking for is "palindrome." Feb 9, 2010 at 14:40
  • What's the infinite loop in the last line of main? Also, who's deallocating the memory reserved by those new?
    – Manuel
    Feb 9, 2010 at 14:42

3 Answers 3

4

the both strtok calls

buffer = strtok(buffer, " ");
poly_buffer = strtok(poly_buffer," ");

are interfering with each other, you need to process them one by one - you cannot do them at the same time because they are sharing static memory in the runtime library. i.e. first do strtok(buffer," ") strtok(NULL, " ") until end, then do strtok( poly_buffer, " ")///

see runtime reference doc for strtok

0
2

If you are using C++, why on Earth would you use strtok? Use a stringstream to tokenise and a vector to contain the words:

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  istringsream is( "here are some words" );
  string word;
  vector <string> words;
  while( is >> word ) {
    words.push_back( word );
  }
  for ( unsigned int i = 0; i < words.size(); i++ ) {
    cout << "word #" << i << " is " << words[i] << endl;
  }
}
0
2

From the man page for strtok, strtok_r:

"Avoid using these functions."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.