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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Numeric String
Click For Summary
SUMMARY

The discussion focuses on determining if a string, specifically a 3-character passCode, contains a numeric digit using C++. Participants suggest implementing a "for" loop to iterate through each character of the string and check for digits. The boolean variable hasDigit should be set to true upon detecting the first digit, allowing for an early exit from the loop. Additionally, the use of the std::string library is recommended for its built-in functions that simplify string manipulation.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with the std::string library in C++
  • Knowledge of control structures, specifically "for" loops
  • Basic understanding of character data types and digit checking
NEXT STEPS
  • Implement a "for" loop to check for digits in a string in C++
  • Explore the std::string library functions such as find_first_of() and substr()
  • Learn about character data types and their manipulation in C++
  • Review best practices for string handling in C++ versus C-style strings
USEFUL FOR

Beginner and intermediate C++ programmers, software developers working with string manipulation, and anyone looking to enhance their understanding of control structures in C++.

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.
 

Similar threads

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