Find the answer to your Linux question:
Results 1 to 2 of 2
i want my code do something like this example: when user input first time:" yesterday one more" it must be convert into :"yEsTeRdAy OnE MoRe" when user input second time: ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    1

    Help to improve c code(convert sentence)

    i want my code do something like this example:

    when user input first time:" yesterday one more"
    it must be convert into :"yEsTeRdAy OnE MoRe"

    when user input second time: "yesterday one more"
    it must be convert int0 : "YeSTeRdAy OnE MoRe"

    but my code only convert "yesterday one more" into "YESTERDAY ONE MORE"


    PHP Code:
    /*
     * pipe2.c  - two processes communicating both ways by using two
     *            pipes. One process reads input from the user and handles
     *            it. The other process makes some translation of the
     *            input (translates upper-case letters to lower-case),
     *            and hands it back to the first process for printing.
     */

    #include <stdio.h>    /* standard I/O routines.                  */
    #include <unistd.h>   /* defines pipe(), amongst other things.   */
    #include <ctype.h>    /* defines isascii(), toupper(), and other */
                          /* character manipulation routines.        */

    /* function executed by the user-interacting process. */
    void user_handler(int input_pipe[], int output_pipe[])
    {
        
    int c;    /* user input - must be 'int', to recognize EOF (= -1). */
        
    char ch;  /* the same - as a char. */
        
    int rc;   /* return values of functions. */

        /* first, close unnecessary file descriptors */
        
    close(input_pipe[1]);  /* we don't need to write to this pipe.  */
        
    close(output_pipe[0]); /* we don't need to read from this pipe. */

        /* loop: read input from user, send via one pipe to the translator, */
        /* read via other pipe what the translator returned, and write to   */
        /* stdout. exit on EOF from user.                                   */
        
    while ((getchar()) > 0) {
            
    /* note - when we 'read' and 'write', we must deal with a char, */
            /* rather then an int, because an int is longer then a char,    */
            /* and writing only one byte from it, will lead to unexpected   */
            /* results, depending on how an int is stored on the system.    */
            
    ch = (char)c;
            
    /* write to translator */
            
    rc write(output_pipe[1], &ch1);
            if (
    rc == -1) { /* write failed - notify the user and exit. */
                
    perror("user_handler: write");
                
    close(input_pipe[0]);
                
    close(output_pipe[1]);
                exit(
    1);
            }
            
    /* read back from translator */
            
    rc read(input_pipe[0], &ch1);
            
    = (int)ch;
            if (
    rc <= 0) { /* read failed - notify user and exit. */
                
    perror("user_handler: read");
                
    close(input_pipe[0]);
                
    close(output_pipe[1]);
                exit(
    1);
            }
            
    /* print translated character to stdout. */
            
    putchar(c);
        }

        
    /* close pipes and exit. */
        
    close(input_pipe[0]);
        
    close(output_pipe[1]);
        exit(
    0);
    }

    /* now comes the function executed by the translator process. */
    void translator(int input_pipe[], int output_pipe[])
    {
        
    int c;    /* user input - must be 'int', to recognize EOF (= -1). */
        
    char ch;  /* the same - as a char. */
        
    int rc;   /* return values of functions. */

        /* first, close unnecessary file descriptors */
        
    close(input_pipe[1]); /* we don't need to write to this pipe.  */
        
    close(output_pipe[0]); /* we don't need to read from this pipe. */

        /* enter a loop of reading from the user_handler's pipe, translating */
        /* the character, and writing back to the user handler.              */
        
    while (read(input_pipe[0], &ch1) > 0) {
            
    ch;

            
    /* translate any upper-case letter to lowr-case. */
            
    if (isascii(c) && isupper(c))
                
    tolower(c);

            
    ch c;
            
    /* write translated character back to user_handler. */
            
    rc write(output_pipe[1], &ch1);
            if (
    rc == -1) { /* write failed - notify user and exit. */
                
    perror("translator: write");
                
    close(input_pipe[0]);
                
    close(output_pipe[1]);
                exit(
    1);
            }
        }

        
    /* close pipes and exit. */
        
    close(input_pipe[0]);
        
    close(output_pipe[1]);
        exit(
    0);
    }

    /* and finally, the main function: spawn off two processes, */
    /* and let each of them execute its function.               */
    int main(int argccharargv[])
    {
        
    /* 2 arrays to contain file descriptors, for two pipes. */
        
    int user_to_translator[2];
        
    int translator_to_user[2];
        
    int pid;       /* pid of child process, or 0, as returned via fork.    */
        
    int rc;        /* stores return values of various routines.            */

        /* first, create one pipe. */
        
    rc pipe(user_to_translator);
        if (
    rc == -1) {
            
    perror("main: pipe user_to_translator");
            exit(
    1);
        }
        
    /* then, create another pipe. */
        
    rc pipe(translator_to_user);
        if (
    rc == -1) {
            
    perror("main: pipe translator_to_user");
            exit(
    1);
        }

        
    /* now fork off a child process, and set their handling routines. */
        
    pid fork();

        switch (
    pid) {
            case -
    1:        /* fork failed. */
                
    perror("main: fork");
                exit(
    1);
            case 
    0:         /* inside child process.  */
                
    translator(user_to_translatortranslator_to_user); /* line 'A' */
                /* NOT REACHED */
            
    default:        /* inside parent process. */
                
    user_handler(translator_to_useruser_to_translator); /* line 'B' */
                /* NOT REACHED */
        
    }

        return 
    0;   /* NOT REACHED */

    CAN YOU HELP ME IMPROVE OBOVE CODE

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Just from casual inspection of the code, it looks as though your code translates all to lower case, not to alternating upper and lower case as you want. Yet you say the output is all upper case. But, in fact, there is no call to toupper() anywhere in your code!

    In my opinion, your first step is to write a simple program (just the main() function) which reads from standard input, translates, and writes to standard output. That way you can concentrate on the problem itself.

    It almost looks as though you haven't really tried to solve the translation problem itself, but wish us to solve it for you. You'll learn more by doing it yourself, and then asking us why a particular piece of code doesn't work.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...