Tag Archives: char

Python || Using If Statements & String Variables

As previously mentioned, you can use “int” and “float” to represent numbers, but what if you want to store letters? Strings help you do that.

==== SINGLE CHAR ====

This example will demonstrate a simple program using strings, which checks to see if the user entered the correctly predefined letter.

Notice in line 11 I declare the string data type, naming it “userInput.” I also initialized it as an empty variable. In line 23 I used an “If/Else Statement” to determine if the user entered value matches the predefined letter within the program. I also used the “OR” operator in line 23 to determine if the letter the user entered into the program was lower or uppercase. Try compiling the program simply using this
if (letter == 'a') as your if statement, and notice the difference.

The resulting code should give this as output

Please try to guess the letter I am thinking of: A
You have guessed correctly!

==== CHECK IF LETTER IS UPPER CASE ====

This example is similar to the previous one, and will check if a user entered letter is uppercase.

Notice in line 21, an If statement was used, which checks to see if the user entered data falls between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)

IF ('letter' is equal to or greater than 'A') AND ('letter' is equal to or less than 'Z')

THEN it is an uppercase letter

The resulting code should give this as output

Please enter an UPPERCASE letter: g
Sorry, 'g' is not an uppercase letter..

==== CHECK IF LETTER IS A VOWEL ====

This example will utilize more if statements, checking to see if the user entered letter is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.

This program should be very straight forward, and its basically checking to see if the user entered data is the letter A, E, I, O, U or Y.

The resulting code should give the following output

Please enter a vowel: K
Sorry, 'K' is not a vowel..

==== HELLO WORLD v2 ====

This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.

The following is similar to the other examples listed on this page, except we display the entire string instead of just simply the first character.

The resulting code should give following output

Please enter a sentence: Hello World!
You Entered: 'Hello World!'

C++ || Convert Numbers To Words Using A Switch Statement

This program demonstrates more practice using arrays and switch statements.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
Cin.get
Isdigit
For loops
While Loops
Switch Statements - How To Use

Using “cin.get(),” this program first asks the user to enter in a number (one at a time) that they wish to translate into words. If the text which was entered into the system is a number, the program will save the user input into an integer array. If the text is not a number, the input is discarded. After integer data is obtained, a for loop is used to traverse the integer array, passing the data to a switch statement, which translates the number to text.

This program is very simple, so it does not have the ability to display any number prefixes. As a result, if the number “1858” was entered into the system, the program would output the converted text: “One Eight Five Eight.”


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 four separate times to display different output

======= Run #1 =======

Enter number: 77331

Seven Seven Three Three One

======= Run #2 =======

Enter number: 234-43-1275

Two Three Four Four Three One Two Seven Five

======= Run #3 =======

Enter number: 1(800) 123-5678

One Eight Zero Zero One Two Three Five Six Seven Eight

======= Run #4 =======

Enter number: This 34 Is 24 A 5 Number 28

Three Four Two Four Five Two Eight

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..

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..

C++ || Snippet – How To Read & Write Data From A User Specified Text File

This page will consist of a demonstration of a simple quadratic formula program, which highlights the use of the input/output mechanisms of manipulating a text file. This program is very similar to an earlier snippet which was presented on this site, but in this example, the user has the option of choosing which file they want to manipulate. This program also demonstrates how to read in data from a file (numbers), manipulate that data, and output new data into a different text file.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Fstream
Ifstream
Ofstream
Working With Files
C_str() - Convert A String To Char Array Equivalent
Getline - String Version

Note: The data file that is used in this example can be downloaded here.

Also, in order to read in the data .txt file, you need to save the .txt file in the same directory (or folder) as your .cpp file is saved in. If you are using Visual C++, this directory will be located in

Documents > Visual Studio 2010 > Projects > [Your project name] > [Your project name]


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

Please enter the name of the file: INPUT_Quadratic_programmingnotes_freeweq_com.txt

For the numbers
a = 2
b = 4
c = -16

root 1 = 2
root 2 = -4

Press ENTER to continue...

C++ || Class & Input/Output – Display The Contents Of A User Specified Text File To The Screen

The following is another intermediate homework assignment which was presented in a C++ programming course. This program was assigned to introduce more practice using the class data structure, which is very similar to the struct data structure.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Header Files - How To Use Them
Class - What Is It?
How To Read Data From A File
String - Getline
Array - Cin.Getline
Strcpy - Copy Contents Of An Array
#Define

This program first prompts the user to input a file name. After it obtains a file name from the user, it then attempts to display the contents of the user specified file to the output screen. If the file could not be found, an error message appears. If the file is found, the program continues as normal. After the file contents finishes being displayed, a summary indicating the total number of lines which has been read is also shown to the screen.

This program was implemented into 3 different files (two .cpp files, and one header file .h). So the code for this program will be broken up into 3 sections, the main file (.cpp), the header file (.h), and the implementation of the functions within the header file (.cpp).

Note: The data file that is used in this example can be downloaded here.

Also, in order to read in the data .txt file, you need to save the .txt file in the same directory (or folder) as your .cpp file is saved in. If you are using Visual C++, this directory will be located in

Documents > Visual Studio 2010 > Projects > [Your project name] > [Your project name]

======== FILE #1 – Main.cpp ========

======== FILE #2 – CFileDisp.h ========

Remember, you need to name the header file the same as the #include from the Main.cpp file. This file contains the function declarations, but no implementation of those functions takes place here.

======== FILE #3 – CFileDisp.cpp ========

This is the function implementation file for the CFileDisp.h class. This file can be named anything you wish as long as you #include “CFileDisp.h”


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

C++ || Char Array – Palindrome Checker Using A Character Array, ToUpper, Strlen, Strcpy, & Strcmp

The following is a palindrome checking program, which demonstrates more use of char array’s, ToUpper, Strlen, & Strcmp.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
How to convert text in a char array from lower to uppercase
How to reverse a character array
Palindrome - What is it?
Strlen
Strcpy
Strcmp
While Loops
For Loops
Constant Variables
Setw

Using a constant value, by default, this program first asks the user to enter 5 words and/or sentences that they want to compare for similarity. If the text which was entered into the program is a palindrome, the program will prompt a message to the user via cout. This program determines similarity by using the strcmp function, to compare two arrays together. This program also demonstrates how to reverse a character array, aswell as demonstrates how to convert all the text which was placed into the char array from lower to UPPERCASE.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Click here to see how cin.getline works.

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 2 seperate times to demonstrate different output)

====== RUN 1 ======

Welcome to the Palindrome Check System!
Please enter 5 word(s) to check for similarity:
#1: SteP oN nO PEts
#2: My ProGramminG NoTeS
#3: RaTs liVE ON No eViL StaR
#4: ABLe wAs I ErE I sAw ElBa
#5: LiVE Non Evil

------------------------------------------------------------
This is what you entered into the system:
Text #1: SteP oN nO PEts
Text #2: My ProGramminG NoTeS
Text #3: RaTs liVE ON No eViL StaR
Text #4: ABLe wAs I ErE I sAw ElBa
Text #5: LiVE Non Evil

------------------------------------------------------------
Here are the palindrome's:
Text #1: SteP oN nO PEts is a palindrome
Text #3: RaTs liVE ON No eViL StaR is a palindrome
Text #4: ABLe wAs I ErE I sAw ElBa is a palindrome
Text #5: LiVE Non Evil is a palindrome

====== RUN 2 ======

Welcome to the Palindrome Check System!
Please enter 5 word(s) to check for similarity:
#1: today Is Great
#2: Tomorrow is Foriegn
#3: Sunday Brunch
#4: Hello SkiPper
#5: Mayday Ship DowN!

------------------------------------------------------------
This is what you entered into the system:
Text #1: today Is Great
Text #2: Tomorrow is Foriegn
Text #3: Sunday Brunch
Text #4: Hello SkiPper
Text #5: Mayday Ship DowN!

------------------------------------------------------------
Here are the palindrome's:
There were no palindrome's found in the current list!

C++ || Char Array – Convert Text Contained In A Character Array From Lower To UPPERCASE

This program demonstrates how to switch text which is contained in a char array from lower to UPPERCASE. This program also demonstrates how to convert all of the text contained in a char array to lower/UPPERCASE.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
Islower
Isupper
Tolower
Toupper
Strlen
While Loops
For Loops
Constant Variables
Setw

Using a constant integer value, this program first asks the user to enter in 3 lines of text they wish to convert from lower to UPPERCASE. Upon obtaining the information from the user, the program then converts all the text which was placed into the character array from lower to uppercase in the following order:

(1) Switches the text from lower to UPPERCASE
(2) Converts all the text to UPPERCASE
(3) Converts all the text to lowercase

After each conversion is complete, the program displays the updated information to the screen via cout.

NOTE: On some compilers, you may have to add #include < cstdlib>, #include < cctype>, and #include < cstring> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Click here to see how cin.getline works.

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

Please enter 3 line(s) of text you wish to convert from lower to UPPERCASE:
#1: I StriKe hiM a heAVy bloW.
#2: When cAn the neRve ShinE?
#3: My Programming Notes.

------------------------------------------------------------
This is what you entered into the system:
Text #1: I StriKe hiM a heAVy bloW.
Text #2: When cAn the neRve ShinE?
Text #3: My Programming Notes.

------------------------------------------------------------
This is the information switched from lower to UPPERCASE:
Text #1: i sTRIkE HIm A HEavY BLOw.
Text #2: wHEN CaN THE NErVE sHINe?
Text #3: mY pROGRAMMING nOTES.

------------------------------------------------------------
This is the information converted to all UPPERCASE:
Text #1: I STRIKE HIM A HEAVY BLOW.
Text #2: WHEN CAN THE NERVE SHINE?
Text #3: MY PROGRAMMING NOTES.

------------------------------------------------------------
This is the information converted to all lowercase:
Text #1: i strike him a heavy blow.
Text #2: when can the nerve shine?
Text #3: my programming notes.

C++ || Using If Statements, Char & String Variables

As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.

==== SINGLE CHAR ====

This example will demonstrate a simple program using char, which checks to see if you entered the correctly predefined letter.

Notice in line 13 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 19 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 19 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a') as your if statement, and notice the difference.

The resulting code should give this as output

Please try to guess the letter I am thinking of: K
Sorry, that was not the correct letter I was thinking of

==== CHECK IF LETTER IS UPPER CASE ====

This example is similar to the previous one, and will check if a letter is uppercase.

Notice in line 19, an If statement was used, which checked to see if the user entered data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)

IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')

THEN it is an uppercase letter

C++ uses ASCII codes to determine letters, so from looking at the table, the letter ‘A’ would equal ASCII code number 65, letter ‘B’ would equal ASCII code number 66 and so forth, until you reach letter Z, which would equal ASCII code number 90. So in literal terms, the program is checking to see if the user input is between ASCII code number 65 thru 90. If it is, then the number is an uppercase letter, otherwise it is not.

The resulting code should give this as output

Please enter an UPPERCASE letter: G
G is an uppercase letter

==== CHECK IF LETTER IS A VOWEL ====

This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.

This program should be very straight forward, and its basically checking to see if the user inputted data is the letter A, E, I, O, U or Y.

The resulting code should give the following output

Please enter a vowel: O
Correct, O is a vowel!

==== HELLO WORLD v2 ====

This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.

Notice in line 10 we have to add “#include string” in order to use the getline function, which is used on line 17. Rather than just simply using the “cin” function, we used the getline function instead to read in data. That is because cin is unable to read entire sentences as input. So in line 17, the following code reads a line from the user input until a newline is entered.

The resulting code should give following output

Please enter a sentence: Hello World!
You Entered: Hello World!