Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Outputing Capitals

Posted on 2003-11-20
4
Medium Priority
?
289 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 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

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

688 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