C/C++ C++: Determining If a String Contains A Numeric Digit

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Numeric String
Click For Summary
To determine if a 3-character passCode contains a digit, set the boolean variable hasDigit to true if a digit is found. A suggested approach involves using a "for" loop to iterate through each character of the passCode string. The loop should check if each character is a digit, and if a digit is detected, hasDigit should be set to true, and the loop should exit early. The loop's index should be less than the size of the passCode string. Additionally, while some users prefer traditional character arrays or other string types, utilizing std::string is recommended for its built-in functions like substr(), replace(), and find_first_of(), which can simplify the implementation.
ineedhelpnow
Messages
649
Reaction score
0
Set hadDigit to true if the 3-character passCode contains a digit.

Sample program:

Code:
#include 
#include 
#include 
using namespace std;

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

   passCode = "abc";

   <student code>

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

   return 0;
}

I'm super lost on this. I could definitely use some help/hints. Thanks
 
Technology news on Phys.org
One way to do this would be to use a "for" loop and check each character in the string sequentially to see if it is a digit or not. Once the first digit is detected, then hasDigit should be set to true and the loop should be broken, otherwise the loop will have checked all characters in the string and hasDigit will remain false.

For the "for" loop you will need the size of "passCode" as the value that the loop's index should be less than.

Also, it appears you need to include some libraries...
 
I can't say I am a fan of std::string in C++ as there are people who still insist on using char* or CString if you are using Microsoft stuff (and all the compatibility issues of these "string" classes).

However, if you are already using std:string, I would suggest you look at the reference of that library:

string - C++ Reference

It already provided handy functions like substr(), replace() and find_first_of() that answer a number of questions you posted fairly simply.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

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