Solved

Outputing Capitals

Posted on 2003-11-20
4
279 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 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now