Solved

Outputing Capitals

Posted on 2003-11-20
4
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 75 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

617 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