By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

can anyone help me in correcting this code?

P: 4
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

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 ;}
c1 = in_c1 + 19;

// Code for function decodeLower(....)
char decodeLower(unsigned char in_c2)
{char c2;
if (in_c2 + 9 > 'z')
c2 = in_c2 - 'z' + 'a' - 1 ;
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);

// Write a loop for decoding characters of inString.
for(i=0,j=0; inString[i] != '\0'; i++)
{c = inString[i];
if ( c >= 'A' && c <= 'Z' )
{ up_count++;
outString[j] =decodeUpper(c);
else{ if ( c >= 'a' && c <= 'z' )
outString[j] = decodeLower(c);
else {if ( c >= '0' && c <= '9' )
outString[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 ,
Nov 5 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.