473,408 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

can anyone help in correcting this code?

vv1
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[i] != '\0'; i++)
{c = inString[i];
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));
}

Nov 5 '06 #1
1 1602
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[i] != '\0'; i++)
{c = inString[i];
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 -- tm*@nj.net -- AB2SB

Nov 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gsb | last post by:
Don't know if this is the right place to post this JavaScript issue. If not, could someone point me in the right direction please. I am trying to make a "cross browser compliant" floating...
12
by: craig | last post by:
Quick question for the experts... Whenever I set the Image property of a PictureBox on one of my windows forms to a PNG graphic file on my hard drive, I get the following runtime error when I...
0
by: dio_mack | last post by:
MINIMIZE RISK BY CONFIRMING IDENTITY OF INDIVIDUALS Obtain the confidence you need to move forward quickly and accurately in business, institutional and personal matters with a full-spectrum check-...
66
by: QuantumG | last post by:
Decompilation is the process of recovering human readable source code from a program executable. Many decompilers exist for Java and .NET as the program executables (class files) maintain much of...
0
by: vp1 | last post by:
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, ...
1
by: srinivasarv | last post by:
Dear Friends, kindly help me in correcting the following code This code is for printing the report from the combo box after selection which connects to different reports as selected. I have...
0
by: Blubaugh, David A. | last post by:
To All, Has anyone worked with the F2PY generator? This is something that is supposedly built within numpy and scipy for the Python environment. I was wondering if anyone has encountered any...
1
by: Blubaugh, David A. | last post by:
Pauli, Yes, I am utilizing the windows environment. I cannot install f2py. I obtain the following error when I try to execute the setup.py file within the f2py folder located within the...
3
by: graphicssl | last post by:
Okay, so first of all, I'm a designer first and a light coder second (I'm only really trained with HTML and CSS). So I apologize for having to post about something that's probably super-trivial! ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.