Thread: any suggestion - pointer, strtok, function etc

  1. #1
    Unregistered
    Guest

    any suggestion - pointer, strtok, function etc

    Hi to all,

    I have done the following code:

    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>

    void decodeMorse(char *tokenPointer, char *plainText){

    if((strcmp(tokenPointer,".-"))==0)
    plainText="A";

    if((strcmp(tokenPointer,"-..."))==0)
    plainText="B";
    }

    void main(){
    char inputStr[256];
    char outputStr[256];
    char *tokenPointer;
    char *token=NULL;
    int c;
    int i=0;

    while((c=getchar())!=EOF)
    inputStr[i++]=c;
    inputStr[i]='\0';

    tokenPointer=strtok(inputStr," ");

    /* we may have to include the following statements here too :
    decodeMorse(tokenPointer,token);
    strcat(outputStr, token); */ /* not really sure ****/

    /*any suggestion??? ****/

    while(tokenPointer != NULL){
    strcat(outputStr, " ");
    decodeMorse(tokenPointer,token);
    strcat(outputStr, token);

    tokenPointer=strtok(NULL, " ");

    }
    printf("%s\n",outputStr);
    getchar();
    }


    In my system, I can compile this, but when I run it results exception.

    Any pointers would be highly appreciated.

    Damar

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    char *decodeMorse ( char *tokenPointer ) {
        char *result = "?";     // some unknown morse sequence...
        if((strcmp( tokenPointer,".-" ))==0)
            result = "A";
        if((strcmp( tokenPointer,"-..." ))==0)
            result = "B";
        return result;
    }
    
    int main () {           // never void
        char inputStr[256];
        char outputStr[256];
        char *morse;
        char *ascii;
        char *seps = " \n"; // spaces and newlines are separators
    
        /* read a whole line of morse */
        fgets( inputStr, sizeof(inputStr), stdin );
    
        outputStr[0] = '\0';    // empty the string
    
        /* split the line at each space */
        for ( morse = strtok(inputStr,seps) ;
              morse != NULL ;
              morse = strtok(NULL,seps) ) {
            ascii = decodeMorse( morse );
            strcat( outputStr, " " );
            strcat( outputStr, ascii );
        }
        printf( "%s\n",outputStr );
    
        getchar( );
        return 0;   // all successful programs return 0
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM