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
AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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