0

I am working on a programming exercise, "Count of camel case characters" using C++. The goal of the exercise is to count the number of upper-case letters in a given string (what the exercise calls "camel case").

So given the following two inputs:

  • ckjkUUYII
  • HKJT

I would expect to get the following counts respectively:

  • 5
  • 4

Based on the code I've include below, however, I am instead getting:

  • 0
  • 5

This is clearly incorrect, but I've having difficulty isolating the problem in my code. How can I reason through this problem, or debug my error?

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int t;cin>>t;
    while(t--)
    {
        int res=0;
        string str;
        getline(cin,str);
        int len= str.length();
        for(int i=0;i<len;i++)
        {
            int c=str[i];
            if(isupper(c))
            res=res+1;
        }
        cout<<res<<endl;
    }
    //return 0;
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
shivanik6z
  • 19
  • 4
  • Welcome to Stack Overflow. First, it is worth noting that what the exercise problem calls "alphabets in camel case" is actually _upper case letters_. I have updated your question to reflect this. Camel case is a convention for compound identifiers such as `camelCase` (note the "hump" in the middle due to the upper case "C"). I have also moved your sample strings, expected output, and actual output to the top, and expanded on your question to hopefully help improve the types of responses you get. – Jeremy Caney May 06 '20 at 20:11
  • Note that there is a major bug in your code. `isupper()` is supposed to take a character, **cast to `unsigned char`**. The rationale here is that, due to C heritage, `isupper` is supposed to work on either a valid character *or EOF*, which is *negative*. So the valid input range for characters is 0..255, even if your platform happens to define `char` as *signed* (-128..127 on two's complement). Your code will then give wrong results for non-ASCII-7 inputs, like "Ö" or "Ð" (which *should* register as uppercase, but wouldn't as they are negative, thus interpreted as EOF). – DevSolar May 06 '20 at 20:18

2 Answers2

0

After entering integer value as t, in input buffer newline character is left. So first call of getline gives you empty string as a result. You have to do:

int t;
cin >> t;
cin.ignore();
while (t--) {
  ...
}

to consume newline character, then both calls of getline will return entered strings properly.

rafix07
  • 20,001
  • 3
  • 20
  • 33
-1

The main problem is that you are trying to convert a character into an integer in line 15. An integer can't be either uppercase or lowercase. hence it gives the wrong answer. Simply check isupper(s[i]) as it would give the correct answer.

Consider my code,

#include <bits/stdc++.h>

using namespace std ;
int main() {
    int t ; cin >> t ;
    while(t--) {
        string s ; cin >> s ;
        int cnt = 0 , ln = s.size() ;
        for(int i = 0 ; i < ln ; i ++) {
            if(isupper(s[i])) cnt ++ ;
        }
        cout << cnt << endl ;
    }

    return 0;
}


Nur Bijoy
  • 70
  • 2
  • 7
  • [`isupper( int c )`](https://en.cppreference.com/w/cpp/string/byte/isupper). Yes, `int`, not `char`. See my comment above; your code has the same bug as the OP's. – DevSolar May 06 '20 at 20:21
  • Sorry to say, I can challenge you that it has no bug. I've already tested it on the online judge(GeeksForGeeks) and it was accepted. Don't bad comment without proper logic. – Nur Bijoy May 08 '20 at 07:28
  • I've *linked* the proper logic... but OK, you seem to be more the practical type. Consider, `'ÿ'` / LATIN SMALL LETTER Y WITH DIAERESIS (not yelling at you, just giving the proper Unicode name of the letter). In Latin-1 / Latin-9, this is `'\xff'`. Try this yourself: `islower( '\xff' )` is false, `islower( -1 )` is false, `islower( (unsigned char)'\xff' )` is true. (I am using the hex escape here because handling of source encoding is implementation-defined). Please explain why only the version *with* the cast yields the correct result. – DevSolar May 08 '20 at 10:32
  • Also, [you should not `#include `](https://stackoverflow.com/a/31816096/60281). – DevSolar May 08 '20 at 10:46