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

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Numeric String
Click For Summary
The discussion focuses on determining if a 3-character passCode contains a digit without using loops. The key solution involves utilizing the `isdigit` function from the `<cctype>` header to check each character in the passCode. The suggested code sets the boolean variable `hasDigit` to true if any of the three characters are digits. The use of the boolean operator `||` (or) is highlighted as an efficient way to combine conditions. Additionally, alternatives using `if` statements are discussed, emphasizing that each character must be checked individually to ensure that `hasDigit` reflects the presence of a digit correctly. Overall, the conversation provides guidance on implementing these checks effectively within the constraints given.
needOfHelpCMath
Messages
70
Reaction score
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
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).
 
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.
 
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:
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
51K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K