Solved

Outputing Capitals

Posted on 2003-11-20
4
284 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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 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 while-loops in the C programming language.

785 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