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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Numeric String
AI Thread 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.
 
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...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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