2

I tried to write a C++ function to check if a char is a capital letter in a given string.

Here's my attempt:

#include<iostream>
#include <conio.h>
#include<string>
using namespace std;
int iscapital(char x)
{
 if (x>='A'&&x<='Z')    return 1;

 else  return 0;
}
main()
{
char a[20];int len; int c=0;
cout<<"enter your line: ";
cin>>a;
len=strlen(a);
for (int i=0;i<=len;i++)
iscapital(a[i]);
if (iscapital)
{
    c++;
}

cout<<"capital letter in string is: "<<c;
}
silentsod
  • 8,165
  • 42
  • 40
Ibra OpEd
  • 31
  • 1
  • 1
  • 5
  • 3
    Are you required to write the check code yourself? If not use `isupper` – NathanOliver Jan 04 '17 at 16:35
  • 5
    [`isupper`](http://en.cppreference.com/w/cpp/string/byte/isupper), [`count_if`](http://en.cppreference.com/w/cpp/algorithm/count). – BoBTFish Jan 04 '17 at 16:35
  • 1
    Or the [locale-enabled `isupper`](http://en.cppreference.com/w/cpp/locale/isupper), if you care about other than English ASCII. – Fred Larson Jan 04 '17 at 16:37
  • 1
    @FredLarson - C's `isupper` is also locale-sensitive. It uses the global locale. – Pete Becker Jan 04 '17 at 16:40
  • 1
    shouldn't `main` be `int main()`? Also I see that you're learning, I highly recommend you study `std::string`, stuff like `strlen` `char x[100]` is more like the `C` way and not the `C++` way of doing things. And, you wrote `if(iscapital)` I guess you meant `if(iscapital(a[i]))` – Vinícius Jan 04 '17 at 16:42
  • @PeteBecker: Ah. I guess I should say, "... if you care about other than the default locale." – Fred Larson Jan 04 '17 at 16:44
  • 1) You should include `cstring` for `strlen`. 2) `for (int i = 0; i < len; i++) if (iscapital(a[i])) { c++; }`. 3) `cin >> a` can overflow your buffer. – Unimportant Jan 04 '17 at 16:48
  • @FredLarson - looks like I was a bit too terse in my comment. The global locale defaults to the "C" locale which, as you mentioned, is usually ASCII. But you can **set** the global locale in C and C++ with `set_locale()`. The advantage of C++'s `isupper` etc. is that they're much easier to use when you need different locales in different functions or threads. The C interface can lead to confusion if you need different locales in different parts of your program. – Pete Becker Jan 04 '17 at 17:03
  • There is nothing in standard C or standard C++ that requires `x>='A'&&x<='Z'` to do anything sensible. This works for ASCII because the character set uses contiguous increasing values for capital letters, but other character sets don't do that. It's not like `'0'` ... `'9'`, which are required by the language definition to have contiguous increasing values, which makes `ch - '0'` work to convert digit characters to numeric values. – Pete Becker Jan 04 '17 at 17:07
  • everyone thank you for helping me I solved the problem now :D – Ibra OpEd Jan 04 '17 at 17:12

3 Answers3

2

You are not using iscapital correctly.

for (int i=0;i<=len;i++)
    iscapital(a[i]); // Call the function, ignore the result
if (iscapital)   // <- This is not valid C++
{
    c++;
}

What you want is this

for (int i=0;i<=len;i++)
    if (iscapital(a[i]))
    {
        c++;
    }

As others have commented, look up std::isupper to know if a letter is a capital and std::count, std::count_if to count the number of occurrences of a value or the number of times a condition is true.

Additionally, main should return int and iscapital should return bool. Using int to represent true or false values is outdated and should not be used in new code. Finally, consider using std::string instead of char []. Using character arrays to represent strings is the C way of doing things. C++ uses std::string which many subtle problems.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • thanks a lot for your and I'll look up for std::isupper I didn't know it before , – Ibra OpEd Jan 04 '17 at 17:08
  • This is stupid ... ' Using int to represent true or false values is outdated ... ' .The only advantage bool has is the semantic meaning of your code. If another developer reads your code and sees a return type of bool, he immediately understands that it returns a truth value. That's all .. – Reznicencu Bogdan Jan 04 '17 at 17:23
  • @ReznicencuBogdan Using `int` to represent boolean was done in C before C99 when there was no `bool` type. Is there a situation where it's advantageous to use `int` instead of `bool` to represent the boolean result of a function in C++? – François Andrieux Jan 04 '17 at 17:51
2

Your code should look like this :

int iscapital(char x)
{
       if (x >='A' && x <= 'Z')    return 1;
       else  return 0;
}

int main()
{
  char a[20];int len; int c=0;
  cout<<"enter your line: ";
  cin.getline(a , 20);      
  // Note : ' getline ' will read the entire line written in the console and will stop only at the end line mark...will include and the white spaces .
  // http://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin

  len=strlen(a);
  for (int i = 0;i < len;i++)
  {
    if (iscapital(a[i]))
    {
       c++;
    }
  }
  cout<<"capital letter in string is: "<<c;

  return 0;
 }
Reznicencu Bogdan
  • 902
  • 2
  • 11
  • 28
  • Maybe it's time to get to read some documentation ... In c++ bool true represents a value of 1 (casted to int) // http://stackoverflow.com/questions/2725044/can-i-assume-booltrue-int1-for-any-c-compiler – Reznicencu Bogdan Jan 04 '17 at 17:14
  • According to the standard, you should be safe with that assumption. The C++ bool type has two values - true and false with corresponding values 1 and 0. – Reznicencu Bogdan Jan 04 '17 at 17:15
1

correct your code:

  • IsCapital() should return a bool not an integer.

  • for (int i=0; i<=len; i++) also this you are using a[len] so correct it to:

for (int i = 0; i <len; i++)

  • what is this if (iscapital) { c++;}? this is not how to call function isCapital to call it add () and the parameter.

  • make if(iscapital) inside the loop not outside and as you know your loop for here has only one statement as long as you don't add parenthesis.

so the code will look like:

bool iscapital(char x)
{
    if (x >= 'A' && x <= 'Z')
        return 1;
    else
        return 0;
}

main()
{
    char a[20];
    int len;
    int c = 0;

    cout << "enter your line: ";
    cin >> a;
    len = strlen(a);

    for(int i = 0; i < len; i++)
    {
        if (iscapital(a[i]))
            c++;
    }

    cout << "capital letter in string is: " << c;

    return 0;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • c++ says that there should be a ' int main(int argc , char ** ....) ..... ' . Just 'main' is ambiguos ... – Reznicencu Bogdan Jan 04 '17 at 17:19
  • 1
    ' IsCapital() should return a bool not an integer. ' . Not necesarily ... How come all of you wrote this ? .The Standard allows both using int to return a bool value , and a bool to return a int (casting) . –  Jan 04 '17 at 17:29
  • @AntonVignoli: I not it is not necessarily but as long as it returns only true or false then the right return type is bool, if there's another value other than two then an integer is ok – Raindrop7 Jan 04 '17 at 17:44
  • @IbraOpEd Yes you can return 1 or 0 as it will be converted to true or false when compiled, however for readability you should use return true or false, not return 1 or return 0. – AresCaelum Jan 04 '17 at 17:47