How to check if there is any character in a string?

May I know if there is a way to check any character in a string? I tried isdigit() for a string but it doesn't work ... Thanks in advance!

check any character in a string

What do you mean exactly?
For example say you have a string with a value "I am a string"
What would you like to find out?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iterator>

bool contains_digit( const std::string& str )
{ return std::any_of( std::begin(str), std::end(str), [] ( char c ) { return std::isdigit(c) ; } ) ; }

bool contains_digit_alt( const std::string& str )
{
    for( char c : str ) if( std::isdigit(c) ) return true ;
    return false ;
}

int main()
{
    std::cout << std::boolalpha << contains_digit( "abcd5efgh" ) << '\n' // true
                                << contains_digit( "abcd+efgh" ) << '\n' ; // false

    std::cout << contains_digit_alt( "abcd5efgh" ) << '\n' // true
              << contains_digit_alt( "abcd+efgh" ) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/fcfffab49f85e92f
for example, string s="123456p";
I wanna check if the string contain an alphabet character..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <cctype>

using std::string;
using std::cout;
using std::cin;

bool contains_alpha(const string& s)
{
  for (const char& ch : s)
    if (isalpha(ch))
      return true;

  return false;
}
int main()
{
  cout << "Enter a string: ";
  string input;
  getline(cin, input);
  if (contains_alpha(input))
    cout << "Contains.";
  else
    cout << "Doesn't contain.";

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <locale>

bool has( const std::string& str, std::ctype_base::mask mask )
{
    static const std::locale default_locale ;
    static const auto& ctype = std::use_facet< std::ctype<char> >(default_locale);

    for( char ch : str ) if( ctype.is(mask,ch) ) return true ;
    return false ;
}

bool has_alpha( const std::string& str ) { return has( str, std::ctype_base::alpha ) ; }
bool has_digit( const std::string& str ) { return has( str, std::ctype_base::digit ) ; }
bool has_space( const std::string& str ) { return has( str, std::ctype_base::space ) ; }
bool has_xdigit( const std::string& str ) { return has( str, std::ctype_base::xdigit ) ; }
bool has_alnum( const std::string& str ) { return has( str, std::ctype_base::alnum ) ; }
bool has_blank( const std::string& str ) { return has( str, std::ctype_base::blank ) ; }
bool has_upper( const std::string& str ) { return has( str, std::ctype_base::upper ) ; }
bool has_lower( const std::string& str ) { return has( str, std::ctype_base::lower ) ; }
bool has_punct( const std::string& str ) { return has( str, std::ctype_base::punct ) ; }
bool has_print( const std::string& str ) { return has( str, std::ctype_base::print ) ; }
bool has_cntrl( const std::string& str ) { return has( str, std::ctype_base::cntrl ) ; }
bool has_graph( const std::string& str ) { return has( str, std::ctype_base::graph ) ; }
Wow. thank both of you very much!
Topic archived. No new replies allowed.