Tag Archives: str
C++ || Char Array – Determine If A String Is A Number Or Not
The following is another intermediate homework assignment which was presented in a C++ programming course. This program was assigned to introduce more practice using and manipulating character arrays.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
Character Arrays
Cin.getline
Strlen - Get The Length Of A Char Array
Isalpha
Isspace
This program first prompts the user to input a line of text. After it obtains data from the user, using a for loop, it then displays the the string to the screen one letter (char) at a time. If the current character at that specific array index is a letter, a “flag” is set, indicating that the current word which is being displayed is not a number. If the “flag” is not set, the current word is indeed a number.
This program has the ability to intake multiple words at a time, so for example, if the user input was “Hello World 2012” the program would display the output:
Hello is NOT a number!
World is NOT a number!
2012 is a number..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 24, 2012 // Taken From: http://programmingnotes.org/ // File: isNumber.cpp // Description: Demonstrates checking if a char array is a number // ============================================================================ #include <iostream> #include <cstring> #include <cctype> using namespace std; // function prototype void IsArryANum(char arry[]); int main() { // declare & initialize variables char arry[256]; // obtain data from user cout << "Enter some text to see if its a number or not: "; cin.getline(arry, sizeof(arry)); // getting line cout<<endl; IsArryANum(arry); return 0; }// end of main void IsArryANum(char arry[]) { int notANumber = 0; int length = strlen(arry); // get the length of the char array // and place a [space] at the end of it. Then // set the array index after the [space] to NULL arry[length] = ' '; arry[length + 1] = '\0'; // increment the length to account for the space we just added ++length; for(int x = 0; x < length; ++x) { cout <<arry[x]; // display the char at the current index // if the current char isnt a number, increment counter if(isalpha(arry[x])) { ++notANumber; } // if curerent char is a space, that indicates we are // at the end of the current word, so we // display the results to the user else if(isspace(arry[x])) { if (notANumber > 0) { cout <<"is NOT a number!" <<" There are "<<notANumber<<" letters" <<" in that word...\n"; } else { cout <<"is a number..\n"; } notANumber = 0; } } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
(Note: the code was compiled three separate times to display different output)
====== RUN 1 ======
Enter some text to see if its a number or not: My Programming Notes
My is NOT a number! There are 2 letters in that word...
Programming is NOT a number! There are 11 letters in that word...
Notes is NOT a number! There are 5 letters in that word...====== RUN 2 ======
Enter some text to see if its a number or not: May 30th 2012
May is NOT a number! There are 3 letters in that word...
30th is NOT a number! There are 2 letters in that word...
2012 is a number..====== RUN 3 ======
Enter some text to see if its a number or not: 5 31 2012
5 is a number..
31 is a number..
2012 is a number..