can anyone help in correcting this code?

Discussion in 'C Programming' started by vv1, Nov 5, 2006.

  1. vv1

    vv1 Guest

    Write a C program for reading in a message string (with no blanks)
    and decoding the message. Store the decoded message in another string
    called outString. After decoding is complete, print the decoded
    message. Use the following key for decoding.Convert any numeric
    characters in input (0-9) to a space in the output. Upper case letters
    in input/message string are decoded by adding 19 to their
    ASCII/Integer value. Lower case characters are decoded by adding 9 to
    their ASCII value. All other characters (special) are skipped. Count
    numeric characters in the input. They become spaces in the
    decoded/output string. You also need to count upper and lower case
    characters in the input string. At the end print the count of spaces
    in the output ( number of numeric characters in input). Also
    calculate and print percentage of spaces (blanks) in the OUTPUT
    string.

    I have written the following code but i m not able to get the decoded
    string. rest everything is okie.
    please help in correcting that.

    I would really appreciate the help.

    #include <stdio.h>
    #include <stdlib.h>

    // Code for function decodeUpper(....)
    char decodeUpper(char in_c1)
    {char c1;
    if (in_c1 + 19 > 'Z' )
    {c1 = in_c1 - 'Z' + 'A' - 1 ;}
    else
    c1 = in_c1 + 19;
    return(c1);}

    // Code for function decodeLower(....)
    char decodeLower(unsigned char in_c2)
    {char c2;
    if (in_c2 + 9 > 'z')
    c2 = in_c2 - 'z' + 'a' - 1 ;
    else
    c2 = in_c2 + 9;
    return c2;}

    // Code for function percent(....)
    float percent(int m, int n)
    {float i;
    i = (float)m/n;
    }

    // Function main begins here.

    main ()
    {
    // In C, string is just an array of characters terminated by '\0'.

    char inString[100];
    char outString[100];
    int i, j;
    char c;
    int up_count = 0;
    int lo_count = 0;
    int num_count = 0;
    int length;
    char p;
    // Read input.

    printf("Enter a message string with no blanks.\n",inString);
    scanf("%s",inString);

    // Write a loop for decoding characters of inString.
    for(i=0,j=0; inString != '\0'; i++)
    {c = inString;
    if ( c >= 'A' && c <= 'Z' )
    { up_count++;
    outString[j] =decodeUpper(c);
    j++;}
    else{ if ( c >= 'a' && c <= 'z' )
    {lo_count++;
    outString[j] = decodeLower(c);
    j++;}
    else {if ( c >= '0' && c <= '9' )
    {num_count++;
    outString[j] = ' ';
    j++;}}}
    }


    length = num_count + up_count + lo_count;

    // Print decoded string.
    printf("Decoded Message is :%s\n", outString);

    //Print num_count (count of spaces), length of decoded

    printf("Length Of Decoded Message is %d\n",length);

    printf("Count Of Blanks in Decoded Message is %d \n",num_count);

    //Calculate % by calling function percent and print.

    printf("%f percent of output is blank\n",100 * percent(num_count ,
    length));
    }
     
    vv1, Nov 5, 2006
    #1
    1. Advertising

  2. vv1

    T.M. Sommers Guest

    vv1 wrote:
    > Write a C program for reading in a message string (with no blanks)
    > and decoding the message. Store the decoded message in another string
    > called outString. After decoding is complete, print the decoded
    > message. Use the following key for decoding.Convert any numeric
    > characters in input (0-9) to a space in the output. Upper case letters
    > in input/message string are decoded by adding 19 to their
    > ASCII/Integer value. Lower case characters are decoded by adding 9 to
    > their ASCII value. All other characters (special) are skipped. Count
    > numeric characters in the input. They become spaces in the
    > decoded/output string. You also need to count upper and lower case
    > characters in the input string. At the end print the count of spaces
    > in the output ( number of numeric characters in input). Also
    > calculate and print percentage of spaces (blanks) in the OUTPUT
    > string.
    >
    > I have written the following code but i m not able to get the decoded
    > string. rest everything is okie.
    > please help in correcting that.
    >
    > I would really appreciate the help.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > // Code for function decodeUpper(....)
    > char decodeUpper(char in_c1)
    > {char c1;
    > if (in_c1 + 19 > 'Z' )
    > {c1 = in_c1 - 'Z' + 'A' - 1 ;}
    > else
    > c1 = in_c1 + 19;
    > return(c1);}
    >
    > // Code for function decodeLower(....)
    > char decodeLower(unsigned char in_c2)
    > {char c2;
    > if (in_c2 + 9 > 'z')
    > c2 = in_c2 - 'z' + 'a' - 1 ;
    > else
    > c2 = in_c2 + 9;
    > return c2;}


    These two functions don't seem to satisfy the requirements given
    above. Assuming you were given extra information, and assuming
    that you were told that the addition is to wrap around, then your
    implementation does not work. For the upper case you want
    something like:

    c1 = in_c1 + 19;
    if ( c1 > 'Z' )
    c1 -= 26;

    > // Code for function percent(....)
    > float percent(int m, int n)
    > {float i;
    > i = (float)m/n;
    > }


    This function returns nothing, but is declared as returning a
    float. If you have the function return i, then it is misnamed.

    > // Function main begins here.
    >
    > main ()


    int main(void)

    > {
    > // In C, string is just an array of characters terminated by '\0'.
    >
    > char inString[100];
    > char outString[100];
    > int i, j;
    > char c;
    > int up_count = 0;
    > int lo_count = 0;
    > int num_count = 0;
    > int length;
    > char p;


    p is not used.

    > // Read input.
    >
    > printf("Enter a message string with no blanks.\n",inString);


    The argument instring does not belong here.

    > scanf("%s",inString);


    What happens if the user types in a string longer than can be
    held in inString?

    > // Write a loop for decoding characters of inString.
    > for(i=0,j=0; inString != '\0'; i++)
    > {c = inString;
    > if ( c >= 'A' && c <= 'Z' )


    You should include ctype.h and use isupper(), islower(), and
    isdigit() instead of making your own tests.

    > { up_count++;
    > outString[j] =decodeUpper(c);
    > j++;}
    > else{ if ( c >= 'a' && c <= 'z' )


    You can say 'else if' here, which will keep all the tests at the
    same level.

    > {lo_count++;
    > outString[j] = decodeLower(c);
    > j++;}
    > else {if ( c >= '0' && c <= '9' )
    > {num_count++;
    > outString[j] = ' ';
    > j++;}}}
    > }


    outString needs a nul at the end.

    > length = num_count + up_count + lo_count;
    >
    > // Print decoded string.


    This comment is redundant.

    > printf("Decoded Message is :%s\n", outString);
    >
    > //Print num_count (count of spaces), length of decoded
    >
    > printf("Length Of Decoded Message is %d\n",length);
    >
    > printf("Count Of Blanks in Decoded Message is %d \n",num_count);


    You might want to rename the variable num_count so that it more
    accurately reflects its use.
    >
    > //Calculate % by calling function percent and print.
    >
    > printf("%f percent of output is blank\n",100 * percent(num_count ,
    > length));


    main returns an int, so you need a return here.

    > }


    --
    Thomas M. Sommers -- -- AB2SB
     
    T.M. Sommers, Nov 6, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wilq
    Replies:
    0
    Views:
    1,380
  2. MCP
    Replies:
    11
    Views:
    1,098
    Andrew Thompson
    Jun 11, 2004
  3. zotkara

    Newbie needs help correcting code

    zotkara, Jan 3, 2006, in forum: C Programming
    Replies:
    25
    Views:
    674
    Randy Howard
    Jan 3, 2006
  4. Andrew Poelstra

    Re: can anyone help me in correcting this code?

    Andrew Poelstra, Nov 8, 2006, in forum: C Programming
    Replies:
    8
    Views:
    331
    goose
    Nov 9, 2006
  5. Dr Dav

    correcting code for larger 2D arrays help

    Dr Dav, May 30, 2007, in forum: C Programming
    Replies:
    2
    Views:
    292
    Keith Thompson
    May 30, 2007
Loading...

Share This Page