Hi..all c Expert please help me with token...

This is a discussion on Hi..all c Expert please help me with token... within the C Programming forums, part of the General Programming Boards category; Code: char *reverseWord(char* text){ int i, j, c; char *ptr; char temp[STRING]; int stringLength; stringLength = strlen(text); ptr = text; ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Unhappy Hi..all c Expert please help me with token...

    Code:
    char *reverseWord(char* text){
      int i, j, c;
      char *ptr;
      char temp[STRING];
      int stringLength;
    
      stringLength = strlen(text);
      ptr = text;
      strcpy(temp, text);
    
       for (i=0, j=stringLength-1; j > i; i++, j--)
      {
        c = text[i];
        text[i] = text[j];
        text[j] = c;
     }
      return ptr;
      }
    
      void reverse() {
      int i, j; 
      int c;
      char text[STRING + 1];
      char outputString[STRING + 1];
      char *tokenPtr;
      char temp[STRING + 1];
      int stringLength;
      int *counter;
    
      countOption(2, counter);
    
      stringLength = strlen(text);
      get_input(text);
     
      strcpy(outputString, text);
    
      tokenPtr = strtok(text, " ");
      tokenPtr = reverseWord(tokenPtr);
      strcpy(temp, tokenPtr);
    
      while ((tokenPtr = strtok(NULL, " ")) != NULL) {
      tokenPtr = reverseWord(tokenPtr);
      strcat(temp, tokenPtr);
       }
      j = 0;
      for (i = 0; i < stringLength; i++) { 
        outputString[i] = temp[j];
        j++;
     }
      printf("Reverse String %s\n", outputString);
      }
    Hi...this code dun have compilation error but got some runtime error..when i try to reverse by word..it can't read to next word for example [ This is reverse] Output : [siht is reverse]
    What is my problem??Can anyone help me..Please i will appreciate everythings if you help me..Thanks..
    Last edited by cBegginer; 03-16-2005 at 09:17 PM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    I just tried your code and got Output : [sihTsiesrever]

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    thanks

    hi..thanks for replying me..but i solved the problem oredy..but can i know what compiler u r using??Because i am using borland. I really tested the code and get the wrong answer.But anyway thanks...

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Hi, I compiled with Micrsoft Visual C++ 6..

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > but got some runtime error
    Here's a tip - saying what the error was helps.
    We know you have a problem, we can tell that much simply from the existence of your post.

    My guess is you did this
    Code:
    char *s = "This is reverse";
    reverse( s );
    // or
    reverse( "This is reverse" );
    Whereas you should have tried
    Code:
    char s[] = "This is reverse";
    reverse( s );
    The reason being that it is implementation specific as to whether you can write to string constants (and you do write to them with your strtok call). For some it works, and for others (like you), you get a memory fault of some description.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Thanks...

    Ok...i had try it..and it works..thanks anyway..Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Parsing and Tokens (strtok)
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 04-22-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21