Question:
Help With Some C Programming?
compaq1j30
2009-09-28 19:24:56 UTC
Help with some C Programming (Only C, no C++)
here it is


The purpose of the following program is to count the whitespace, the digits, the uppercase alphabetics and lowercase alphabetics in the input. There are bugs in the program. FIX IT

#include

int main(void)
{
int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;

printf("Please enter a phrase:\n\n");

while((iochar=getchar())!=EOF)
{
if ((iochar=' ')||(iochar='\t')||(iochar='\n'))
{
numwhites++;
putchar(iochar);
}
else
if((iochar>='0')&&(iochar<='9'))
{
numdigits++;
putchar(iochar);
}
else
if(('a'<=iochar)&&(iochar<='z'))
{
numlower++;
putchar(iochar-32);
}
else
if(('A'<=iochar)&&(iochar<='Z'))
{
numupper++;
putchar(iochar);
}
else
putchar(iochar);
}

printf("%d white characters, %d digits, ",numwhites,numdigits);
printf("%d lowercase have been converted to ",numlower);
printf("uppercase and %d uppercase.\n",numupper);

printf("\n\n");


return 0;
}

a. Execute the program in its present form using input for which it is easy to predict the output.
b. Analyze the program from the output obtained through the testing and tentatively make some changes.
c. Retest it until you are sure that the bugs are out.
d. Rewrite the program in more structured way. It should have more than one function.
Three answers:
anonymous
2009-09-28 20:32:57 UTC
Hm....



















Macros defined in ctype.h

isalnum(c) True if c is a letter or digit

isalpha(c) True if c is a letter

isdigit(c) True if c is a digit

iscntrl(c) True if c is a delete character or ordinary control character

isascii(c) True if c is a valid ASCII character

isprint(c) True if c is a printable character

isgraph(c) Like isprint except that the space character is excluded

islower(c) True if c is a lowercase letter

isupper(c) True if c is an uppercase letter

ispunct(c) True if c is a punctuation character

isspace(c) True if c is a space, tab, carriage return, newline, vertical tab, or form-feed

isxdigit(c) True if c is a hexadecimal digit

_toupper(c) Converts c in the range [a-z] to characters [A-Z]

_tolower(c) Converts c in the range [A-Z] to characters [a-z]

toascii(c) Converts c greater than 127 to the range 0-127 by clearing all but the lower 7 bits



U can use switch or nested if:

if(isalnum(char)){

if(isalpha(char)) ... [a..z,A..Z]

{

if(isupper(char)) ... [a..z]

else ... [A..Z]

}

else ...[0...9]

}

else if (isspace(char)) ... whitespace
Mark aka jack573
2009-10-02 23:14:50 UTC
I understand that the instructions of FIX IT are not directed at us. You have been given this program from your instructor to learn things.



What you should do is the instructions that he/she has given you. That is part (a).



Try a simple input such as "A" and you will see whether a digit, an upper case letter, ot a lower case letter is counted.

Then input in a 1 and see what results you get.

Then input in an "a" and see what results you get.

Then input a "\" and see what happens.



As soon as you see the results you will not what is wrong. I saw it from just looking at the code. But I am not going to tell you what is wrong, as you have to learn these things.



Then just as part (b) says, you should tentively change some things. As a hint, you should keep a record of what you change, in case you make it worse, and then you can change it back to what it was.



Then do part (c) testing with the above inputs, and more inputs that have more letters, digits etc. Make sure all of the bugs have been caught. There are not that many. I can see 3 maybe 4, depending on what is really happening in the program.



It should not be too hard if you follow my lead with the inputs for the first few tests.



Anyhow, for part (d), this requires some thinking.



What can you move into a function, and what cant you move to a function, so it is in a more structural way.

Hints:

- The while loop could be put into a function.

- Do you put the first printf statement also in this function?

- Can you move the stuff in the if statements into functions?

- What would you need to do if you did move the stuff in the if statements into functions?

- Or do you make the if statement a function?

- If you did this, would you need parameters?

- Can you put the printf statements at the end in a function?

- Would you need parameters if you did?



Hope that helps you out.



If you can do all of this, and get it right, then you have learnt something, and can then do error checking, or nug hunting with other programs that you make.



If by some strange occurence, you get into trouble, or get lost, you could click on my profile and send me an email (message) and I will try to help you out further.



Good luck with it.
no1home2day
2009-09-29 02:30:33 UTC
This isn't a question. It's a demand "FIX IT!" Sorry, but I don't work for you! You need to ask a question, not demand people to do things for you!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...