C++ - Determining If A String Contains Any Numeric Digits

In summary: Digit) { cout << "Has a digit." << endl; }else { cout << "Has no digit." << endl; }In summary, the code checks to see if the character at index 0 of the passCode is a digit, and if so, sets hasDigit to true. If the character is not a digit, hasDigit is not set and the else clause is executed.
  • #1
needOfHelpCMath
72
0
Set hasDigit to true if the 3-character passCode contains a digit.

Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
	bool hasDigit = false;
	string passCode;
	int valid = 0;

	passCode = "abc";

	if (hasDigit) {
		cout << "Has a digit." << endl;
	}
	else {
		cout << "Has no digit." << endl;
	}

	return 0;
}

Note: I cannot use loops.
 
Technology news on Phys.org
  • #2
Re: i am lost may anyone guide me or show me what to use

You need to be able to determine if a given char value is a digit. In header file cctype, function isdigit is precisely what you want. Here's one solution:

hasDigit = isdigit(passCode[0]) || isdigit(passCode[1]) || isdigit(passCode[2]);

If you don't yet know about the boolean operator || (or), think about using if statement(s).
 
  • #3
Re: i am lost may anyone guide me or show me what to use

You could use
Code:
find_first_of
. There's an reference with an example here.
 
  • #4
Re: i am lost may anyone guide me or show me what to use

johng said:
You need to be able to determine if a given char value is a digit. In header file cctype, function isdigit is precisely what you want. Here's one solution:

hasDigit = isdigit(passCode[0]) || isdigit(passCode[1]) || isdigit(passCode[2]);

If you don't yet know about the boolean operator || (or), think about using if statement(s).

is it possible to use if statements to solve this program
 
Last edited:
  • #5
First you need to find if passCode[0] is a digit:
Code:
hasDigit=false;
if (isdigit(passCode[0])) {
  hasDigit=true;
}
I hope you see that the above code is equivalent to:
Code:
hasDigit=isdigit(passCode[0]);
Next you need to test if passCode[1] is a digit:
Code:
if (isdigit(passCode[1])) {
  hasDigit=true;
}
So the following code tests whether passCode[0] or passCode[1] is a digit:
Code:
hasDigit=isdigit(passCode[0]);
if (isdigit(passCode[1])) {
  hasDigit=true;
}
I hope you see that the above code is not the same as:
Code:
hasDigit=isdigit(passCode[0]);
hasDigit=isdigit(passCode[1]);
Now you can finish with one more if statement.
 

FAQ: C++ - Determining If A String Contains Any Numeric Digits

How can I check if a string contains any numeric digits in C++?

To determine if a string contains any numeric digits in C++, you can use the std::isdigit() function. This function takes in a single character and returns a boolean value indicating whether it is a numeric digit or not. You can loop through each character in the string and use this function to check if it is a digit.

What is the difference between std::isdigit() and std::isalnum() in C++?

The std::isdigit() function only checks if a character is a numeric digit, while the std::isalnum() function checks if a character is either a letter or a numeric digit. So, if you want to specifically check for numeric digits, you should use std::isdigit().

How can I handle strings that contain non-numeric characters in C++?

You can use a combination of std::isdigit() and other string manipulation functions, such as std::find_if_not(), to check for non-numeric characters in a string. If you find a non-numeric character, you can handle it accordingly in your code.

Is there a way to determine the position of the first numeric digit in a string in C++?

Yes, you can use the std::find_if() function to find the first occurrence of a numeric digit in a string. This function takes in two iterators and a predicate function, which in this case would be std::isdigit(). It will return an iterator pointing to the first numeric digit, which you can then use to get the position in the string.

Can I use regular expressions to check for numeric digits in a string in C++?

Yes, C++ has a std::regex library that allows you to use regular expressions to search for patterns in strings. You can use the std::regex_search() function to check if a string contains any numeric digits. However, this may not be as efficient as using the std::isdigit() function, so it should be used with caution.

Similar threads

Replies
2
Views
6K
Replies
1
Views
2K
Replies
3
Views
5K
Replies
1
Views
5K
Replies
22
Views
3K
Replies
5
Views
2K
Replies
8
Views
2K
Back
Top