hi everyone just starting ansi c but i seem to be having problems i have to make a program which checks if the user input is uppercase,lowercase or digit which i have done but for some reason i cant seem to do character counts ive tried one way but it doesnt work and im confused lol but my teacher wnt help as we have already moved on to the next program. so i just want learn what i am doing wrong for future reference.

// portfolio2.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <cctype>
#include <cctype>

int _tmain(int argc, _TCHAR* argv[])
{
  char a; 
  int i;
  int count = 0;
  printf("Please Enter A Character \n");
  scanf( "%c", &a );
   if (isupper (a))
   {
   for(i=0;i<a;i++)
	++count;
    printf("Number of characters in is %d\n", count);
    printf( "You Entered Uppercase Character \n");
   }
   else
   if (islower (a))
   {
	printf( "You Entered A Lowercase Character \n");
   }
   else
   if( isdigit(a))
   {
    printf( "You entered a Number \n");
   }
  return 0;
}

any help will be apperciated thanks

Dani AI

Generated

For : the easiest ways to count uppercase, lowercase and digit characters from multi-character input are (a) read the whole line into a buffer and loop over it, or (b) process stdin one character at a time so you never need to keep the whole input in memory. pointed you in the buffer direction; below is the stream (no-array) method and some safety tips you can apply either way.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int c;
    int upper = 0, lower = 0, digit = 0;

    puts("Enter text (press Enter to finish):");
    while ((c = getchar()) != '\n' && c != EOF) {
        unsigned char uc = (unsigned char)c;   /* important cast */
        if (isupper(uc)) ++upper;
        else if (islower(uc)) ++lower;
        else if (isdigit(uc)) ++digit;
    }

    printf("Uppercase: %d\nLowercase: %d\nDigits: %d\n",
           upper, lower, digit);
    return 0;
}

Troubleshooting and good practices:

  • Cast to unsigned char before passing to isupper/islower/isdigit; those functions require either EOF or an unsigned char value. Passing a plain signed char with negative values is undefined behavior. See the ctype reference for details.
  • If you prefer a buffer approach, use fgets() into a fixed-size array and then iterate; avoid scanf("%s",…) without bounds to prevent overflow.
  • If using Visual Studio and you want to compile as C rather than C++, either save the file with a .c extension or set the project/compiler option to compile as C (Compile as C: /TC).

References:

Recommended Answers

All 4 Replies

>so i just want learn what i am doing wrong for future reference.
The first thing would be compiling a C++ program portfolio2.cpp C code doesn't end in .cpp, they end in .c

yea for some reason VS 2008 seems 2 save it as cpp no idea y quite a few of my m8s have the same problem.

It's unclear what you wish to do.

Do you expect the user to enter a single character? If so there is nothing to count (it's ONE character).

So I'll assume you expect the user to enter more than one character and you are trying to count how many of each type of character (upper, lower, digit) the user entered.

In that case you should read the input into a string and loop over the chars in the string. You will need separate counters for each type.

yea sorry about this lol i may have made it unclear what i wanted to do. the user may enter more than 1 character and then the program should say u entered eg uppercase characters etc.
then i want to be able to count how many characters were entered. thanks for the help so far. can i do this without storing it in an array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.