Tag Archives: c++

C++ || How To Pad Left, Pad Right & Pad Center A String Of Fixed Length Using C++

The following is a module with functions which demonstrates how to pad left, pad right, and pad center a string of a fixed length using C++.

The functions demonstrated on this page left, right, and center aligns the characters in a string by padding them on the left and right with a specified character, of a specified total length.

The returned string is padded with as many padding characters needed to reach a length of the specified total width.

The padding character is user defined, but if no padding character is specified, the string is padded using a whitespace (‘ ‘).


1. Pad Left

The example below demonstrates the use of ‘Utils::padLeft‘ to right align the characters in a string by padding them on the left with a specified character, of a specified total length.

In this example, the default padding character is used to pad the string, which is a whitespace (‘ ‘).


2. Pad Right

The example below demonstrates the use of ‘Utils::padRight‘ to left align the characters in a string by padding them on the right with a specified character, of a specified total length.

In this example, the default padding character is used to pad the string, which is a whitespace (‘ ‘).


3. Pad Center

The example below demonstrates the use of ‘Utils::padCenter‘ to center align the characters in a string by padding them on the left and right with a specified character, of a specified total length.

In this example, the default padding character is used to pad the string, which is a whitespace (‘ ‘).


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Join & Concatenate An Array/Vector/Container Into A Delimited String Using C++

The following is a module with functions which demonstrates how to join and concatenate an array/vector/container into a delimited string using C++.

The function demonstrated on this page joins a container and returns the concatenated string according to the separator(s). The delimiter separator is included in the returned string only if there is more than one element.

The separator can be either a single character or multiple characters. If no separating characters are specified, the string is joined using a comma (‘,’).


1. Join – Integer Array

The example below demonstrates the use of ‘Utils::join‘ to join an array into a delimiter separated string.

In this example, the default separator is used to join the array, which is a comma (‘,’).


2. Join – String Vector

The example below demonstrates the use of ‘Utils::join‘ to join a vector into a delimiter separated string.

In this example, a custom separator is used to join the vector.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Replace A Letter With Its Alphabet Position Using C++

The following is a module with functions which demonstrates how to replace a letter with its alphabet position using C++.


1. Replace With Alphabet Position

The example below demonstrates the use of ‘Utils::getAlphabetPosition‘ to replace a letter with its alphabet position.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || Simple Palindrome Checker Using A Stack & Queue Using C++

The following is a program which demonstrates how to use a stack and a queue to test for a palindrome using C++.

The program demonstrated on this page is an updated version of a previous program of the same type. This program is great practice for understanding how the two data structures work.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Template Classes - What Are They?
Stacks
Queues
LIFO - Last In First Out
FIFO - First In First Out


1. Overview

This program first asks the user to enter in text which they wish to compare for similarity. The data is then saved into the system using the “enqueue” and “push” functions available within the queue and stack classes. After the data is obtained, a while loop is used to iterate through both classes, checking to see if the characters at each location within both classes are the same. If the text within both classes are the same, it is a palindrome.


2. Palindrome Checker

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:

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

Enter in some text to see if its a palindrome: StEP on No pETS

StEP on No pETS is a palindrome!

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

Enter in some text to see if its a palindrome: Hello World

Hello World is NOT a palindrome..

====== RUN 3 ======

Enter in some text to see if its a palindrome: Kenneth, Jennifer, Lynn, Sole

Kenneth, Jennifer, Lynn, Sole is NOT a palindrome..

C++ || How To Sort An Array/Vector/Container With Multiple Sorting Conditions Using C++

The following is a module with functions which demonstrates how to sort an array/vector/container with multiple sorting conditions using C++.

The function demonstrated on this page is a wrapper for the std::sort function, which means it works for any container supported by std::sort.

This function accepts multiple sorting conditions (comparison functions), which allows for complex array sorting much like the SQL/LINQ ‘Order By’ operation.


1. Simple Array – Ascending

The example below demonstrates the use of Utils::sortBy to sort a simple array.

In this example, no sorting conditions are specified. In this case, the array is sorted in ascending order.


2. Simple Array – Descending

The example below demonstrates the use of Utils::sortBy to sort a simple array.

In this example, a sorting condition (comparison function) is specified. In this case, the array is sorted in descending order.

Note: In the example below, a lambda is used, though it is not required. A regular function can be used here as well.


3. Object Vector – Multi Key Sort

The example below demonstrates the use of Utils::sortBy to sort an object vector.

In this example, multiple sorting conditions (comparison functions) are specified. In this case, the vector is sorted by multiple object properties.

When multiple sorting conditions are specified, the sequence is sorted in the order in which the conditions are supplied (FIFO).

Note: In the example below, lambda functions are used, though they are not required. Regular functions can be used here as well.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Get The Next & Previous Multiple Of A Number Using C++

The following is a module with functions which demonstrates how to get the next and previous multiple of a number using C++.

If a number is already a multiple, there is a parameter that allows you to specify if it should be rounded or not.

Click here for sample code demonstrating how to round a number by a specific amount.


1. Get Next Multiple – Include Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will not be rounded up to the next multiple.


2. Get Next Multiple – Skip Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will be rounded up to the next multiple.


3. Get Previous Multiple – Include Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will not be rounded down to the previous multiple.


4. Get Previous Multiple – Skip Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will be rounded down to the previous multiple.


5. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!


6. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Round A Number To The Nearest X Using C++

The following is a module with functions which demonstrates how to round a number to the nearest X using C++.

This function has the ability to either round a number to the nearest amount, always round up, or always round down. For example, when dealing with money, this is good for rounding a dollar amount to the nearest 5 cents.

This function can be used to round a number up or down by any step amount.


1. Round – Nearest

The example below demonstrates the use of ‘Utils::roundAmount‘ to round a number to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


2. Round – Up

The example below demonstrates the use of ‘Utils::roundAmount‘ to always round a number up to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


3. Round – Down

The example below demonstrates the use of ‘Utils::roundAmount‘ to always round a number down to the nearest 5 cents.

The optional function parameter determines they type of rounding to perform.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Split & Batch An Array/Vector/Container Into Smaller Sub-Lists Of N Size Using C++

The following is a module with functions which demonstrates how to split/batch an array/vector/container into smaller sublists of n size using C++.

The function demonstrated on this page is a template, so it should work on containers of any type. It uses a simple for loop to group items into batches.


1. Partition – Integer Array

The example below demonstrates the use of ‘Utils::partition‘ to group an integer array into batches.


2. Partition – String Vector

The example below demonstrates the use of ‘Utils::partition‘ to group a string vector into batches.


3. Partition – Custom Object Vector

The example below demonstrates the use of ‘Utils::partition‘ to group a custom object vector into batches.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Shuffle & Randomize An Array/Vector/Container Using C++

The following is a module with functions which demonstrates how to randomize and shuffle the contents of an array/vector/container using C++.

The following template function is a wrapper for the std::shuffle function.


1. Shuffle – Integer Array

The example below demonstrates the use of ‘Utils::shuffle‘ to randomize an integer array.


2. Shuffle – String Vector

The example below demonstrates the use of ‘Utils::shuffle‘ to randomize a string vector.


3. Shuffle – Custom Object Vector

The example below demonstrates the use of ‘Utils::shuffle‘ to randomize a custom object vector.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Find The Day Of The Week You Were Born Using C++

The following is a program which demonstrates how to find the day of week you were born using C++.

The program demonstrated on this page is an updated version of a previous program of the same type.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Zeller's congruence
#include Utils.h
A Calendar


1. Overview

This program prompts the user for their name, date of birth (month, day, year), and then displays information back to them via cout. Once the program obtains selected information from the user, it will use simple math to determine the day of the week in which the user was born, and determine the day of the week their current birthday will be for the current calendar year. The program will also display to the user their current age, along with re-displaying their name back to them.


2. When Were You Born?

Note: This program uses functions in a custom .h header file #include “Utils.h”. To obtain the code for that file, click here.


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 the different outputs its able to produce

Please enter your name: My Programming Notes
Please enter the month in which you were born (between 1 and 12): 1
Please enter the day you were born (between 1 and 31): 1
Please enter the year you were born: 2012

Hello My Programming Notes. Here are some facts about you!
You were born January 1 2012
Your birth took place on a Sunday
This year (2021) your birthday will take place on a Friday
You currently are, or will be 9 years old this year!

---------------------------------------------------------------------

Please enter your name: Jennifer
Please enter the month in which you were born (between 1 and 12): 1
Please enter the day you were born (between 1 and 31): 27
Please enter the year you were born: 1991

Hello Jennifer. Here are some facts about you!
You were born January 27 1991
Your birth took place on a Sunday
This year (2021) your birthday will take place on a Wednesday
You currently are, or will be 30 years old this year!

---------------------------------------------------------------------

Please enter your name: Kenneth
Please enter the month in which you were born (between 1 and 12): 7
Please enter the day you were born (between 1 and 31): 28
Please enter the year you were born: 1987

Hello Kenneth. Here are some facts about you!
You were born July 28 1987
Your birth took place on a Tuesday
This year (2021) your birthday will take place on a Wednesday
You currently are, or will be 34 years old this year!

---------------------------------------------------------------------

Please enter your name: Name
Please enter the month in which you were born (between 1 and 12): 12
Please enter the day you were born (between 1 and 31): 35
An error occurred: Invalid birth date entered: '35'

C++ || How To Get The Day Of The Week & The Week Day Name From A Date Using C++

The following is a module with functions which demonstrates how to get the day of the week and the week day name from a given date using C++.

The function demonstrated on this page uses Zeller’s congruence to determine the day of the week from a given date.


1. Week Day

The example below demonstrates the use of ‘Utils::getWeekday‘ to get the day of week for a given date. The following function returns a value from a DayOfWeek enum, which represents the day of the week for the given parameters.

The following are possible values that are returned from this function:


• 0 = Sunday
• 1 = Monday
• 2 = Tuesday
• 3 = Wednesday
• 4 = Thursday
• 5 = Friday
• 6 = Saturday


2. Week Day Name

The example below demonstrates the use of ‘Utils::getWeekdayName‘ to get the name of the day of week for the given date.

The following are possible values that are returned from this function:


• Sunday
• Monday
• Tuesday
• Wednesday
• Thursday
• Friday
• Saturday


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || Simple Multi Digit, Decimal & Negative Number Infix To Postfix Conversion & Evaluation

The following is sample code which demonstrates the implementation of a multi digit, decimal, and negative number infix to postfix converter and evaluator using C++.

The program demonstrated on this page has the ability to convert and evaluate a single digit, multi digit, decimal number, and/or negative number infix equation. So for example, if the the infix equation of (19.87 * -2) was entered into the program, the converted postfix expression of 19.87 -2 * would display to the screen, as well as the final evaluated answer of -39.74.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Convert Infix To Postfix
How To Evaluate A Postfix Expression


1. Overview

The program demonstrated on this page is different from a previous implementation of the same type in that this version does not use a Finite State Machine during the conversion process, which simplifies the implemetation!

This program has the following flow of control:

• Get an infix expression from the user
• Convert the infix expression to postfix & isolate all of the math operators, multi digit, decimal, negative and single digit numbers that are found in the postfix expression
• Evaluate the postfix expression by breaking the infix string into tokens found from the above step
• Display the evaluated answer to the screen

The above steps are implemented below.


2. Infix To Posfix Conversion & Evaluation


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.

The following is sample output.

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: 12/3*9

The Infix expression = 12/3*9
The Postfix expression = 12 3 / 9 *

Calculations:
12/3 = 4
4*9 = 36

Final answer = 36

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: -150.89996 - 87.56643

The Infix expression = -150.89996 - 87.56643
The Postfix expression = -150.89996 87.56643 -

Calculations:
-150.9-87.5664 = -238.466

Final answer = -238.466

====== RUN 3 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

The Infix expression = ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))
The Postfix expression = -4 5 ^ s 1.4 * 23 2 + $ -2.8 - / 1 2 % c 7.28 .1987 * 23 t ^ / *

Calculations:
-4^5 = -1024
sin(-1024) = 0.158533
0.158533*1.4 = 0.221947
23+2 = 25
√25 = 5
5--2.8 = 7.8
0.221947/7.8 = 0.0284547
1%2 = 1
cos(1) = 0.540302
7.28*0.1987 = 1.44654
tan(23) = 1.58815
1.44654^1.58815 = 1.79733
0.540302/1.79733 = 0.300614
0.0284547*0.300614 = 0.00855389

Final answer = 0.00855389

====== RUN 4 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1987 + 1991) * -1

The Infix expression = (1987 + 1991) * -1
The Postfix expression = 1987 1991 + -1 *

Calculations:
1987+1991 = 3978
3978*-1 = -3978

Final answer = -3978

====== RUN 5 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1+(2*((3+(4*5))*6)))

The Infix expression = (1+(2*((3+(4*5))*6)))
The Postfix expression = 1 2 3 4 5 * + 6 * * +

Calculations:
4*5 = 20
3+20 = 23
23*6 = 138
2*138 = 276
1+276 = 277

Final answer = 277

C++ || Circular Array – How To Index Into Array As If It Is Circular Using C++

The following is a module with functions which demonstrates how to index into an array as if it is circular using C++.

This function adjusts a range as circular and ‘wraps around’ the value to become in bounds.


1. Circular Array

The example below demonstrates the use of ‘Utils::circularWrap‘ to index into a vector as if it is circular and adjusts a range as circular so the value become in bounds.

Note: A vector is used in this example, but it is not required.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.

C++ || How To Split & Parse A String Into Tokens With Multiple Delimiters Using C++

The following is a module with functions which demonstrates how to split and parse a string into substring tokens with multiple delimiters using C++.

The function demonstrated on this page parses and splits a string and returns a vector that contains the substring tokens according to the delimiters.

The delimiters can be either a single character or multiple characters. If no delimiting characters are specified, the string is split at whitespace characters.


1. Split – Basic Usage

The example below demonstrates the use of ‘Utils::split‘ to split a string into substring tokens.

In this example, the default delimiter is used to split a string, which is a whitespace.


2. Split – Multiple Delimiters

The example below demonstrates the use of ‘Utils::split‘ to split a string into substring tokens.

In this example, multiple delimiters are used to split a string.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

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.