any suggestion - pointer, strtok, function etc

This is a discussion on any suggestion - pointer, strtok, function etc within the C Programming forums, part of the General Programming Boards category; 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) ...

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

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