Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Outputing Capitals

Posted on 2003-11-20
4
Medium Priority
?
293 Views
Last Modified: 2010-04-15
i have to create a program that determines whether the character is a capital letter or not.
#include <stdio.h>

void main (void){

char  c;
void chara (char);

printf("Please enter a character: ");
scanf("%c", &c);
fflush(stdin);
if ((c>= 'A') && (c<='Z'))
      printf("true\n");
else printf("False\n");
}

This code works fine. however when i try to put it into its own function it does not as shown in the code below.

#include <stdio.h>

void main (void){

char  c;
void chara (char);

printf("Please enter a character: ");
scanf("%c", &c);
fflush(stdin);
chara(char);
}

void chara (char c){
if ((c>= 'A') && (c<='Z'))
      printf("true\n");
else printf("False\n");

}

i want to be able to create a program that reads in a sequence and outputs only uppercase characters.

any ideas? cheers!
0
Comment
Question by:pete420
4 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 225 total points
ID: 9792081
With a bug fix on your function call to chara - the parameter is c - your code works OK in GCC 3.2 and C 7.1. I made you main function return int, because that's what standards like it to do, but otherwise you code was OK.

--------8<--------
#include <stdio.h>

int main() {
char  c;
void chara (char);
printf("Please enter a character: ");
scanf("%c", &c);
fflush(stdin);
chara(/*char*/c);
}

void chara (char c){
if ((c>= 'A') && (c<='Z'))
     printf("true\n");
else printf("False\n");
}
--------8<--------
0
 

Expert Comment

by:Eric-K
ID: 9856232
I don't understand your purpose ... Is your objective to have the result, or to learn how to do it?

If all you want is to convert a string, you can use the standard library function "toupper()" which converts any character to its upper case equivalent.  Thus, you could write the following function to convert a whole string:

/* Note: this code modifies the callers original string */
void CapString(char *string)
{  
     int i;

     for(i=0; string[i]; i++)
     {
         string[i]=toupper(string[i]);
     }
}

if what you wanted was to understand how to implement toupper itself, well you're halfway there with your original coding example ... once you discriminate if a letter is upper or lower case, then instead of printing a true or false, you perform the conversion by adding the difference of the ASCII code value between upper and lower case characters
that is:

char toupper(char c)
{
    if ((c>= 'a') && (c<='z'))
    {
        c += ('A' - 'a');
    }

    return(c);
}


Hope this helps

Eric-K
     
0
 

Author Comment

by:pete420
ID: 9862821
thanx for the post,
the point of the program was to input a sentence then scan through it and only output the capital letters. i had the program working with the post b4 yours but i appreciate the more knowledge you provided.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question