Concept explainers
(Character Testing) Write a
Program Plan:
- mainfunction used to test all the functions.
- Declare character type of variable character.
- printf function used to print the formatted output on console.
- scanf function used to takea formatted inputfrom console.
Program Description:
The following program will input character and test all the character-handling library functions.
Explanation of Solution
Program:
//header file included #include <stdio.h> #include <string.h> #include <stdlib.h> //included main function intmain() { //declare character type variable char character; //display message to user printf("Enter the character:\n"); //input character scanf("%c", &character); //testing each function of character-handling library //isdigit function printf("'%c'%sdigit\n",character, isdigit(character)?"is a ":"is not a "); //isalpha function printf("'%c'%salphabet\n",character, isalpha(character)?"is a ":"is not a "); //isalnum function printf("'%c'%salpha-numeric\n",character, isalnum(character)?"is a ":"is not a "); //isxdigit function printf("'%c'%shexadecimal digit\n",character, isxdigit(character)?"is a ":"is not a "); //islower function printf("'%c'%slowercase letter\n",character, islower(character)?"is a ":"is not a "); //isupper function printf("'%c'%suppercase letter\n",character, isupper(character)?"is a ":"is not a "); //toupper function printf("The uppercase letter of '%c' is '%c'\n",character, toupper(character)); //tolower function printf("The lowercase letter of '%c' is '%c'\n",character, tolower(character)); //isspace function printf("'%c'%sspace\n",character, isspace(character)?"is a ":"is not a "); //iscntrl function printf("'%c'%scontrol character\n",character, iscntrl(character)?"is a ":"is not a "); //ispunct function printf("'%c'%sprinting character of ispunct\n",character, ispunct(character)?"is a ":"is not a "); //isprint function printf("'%c'%sprinting character of isprint\n",character, isprint(character)?"is a ":"is not a "); //isgraph function printf("'%c'%sprinting character of isgraph\n",character, isgraph(character)?"is a ":"is not a "); //exiting from program return 0; }
Explanation:
In the above program, one character is inputted through scanf function. The %c is the format specifier used to input character type value. All the character-handling library function is used to test and print the output. The ternary operator is used inside the printf function to determine if the entered character is a digit, alphanumeric character, hexadecimal digit, lowercase letter or uppercase letter or not.
Sample Output:
Want to see more full solutions like this?
Chapter 8 Solutions
MYPROGRAMMINGLAB WITH PEARSON ETEXT
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (4th Edition)
Starting Out With Visual Basic (7th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Computer Science: An Overview (12th Edition)
- (Rounding Numbers) An application of function floor is rounding a value to the nearestinteger. The statementy = floor(x + .5);will round the number x to the nearest integer and assign the result to y. Write a program that readsseveral numbers and uses the preceding statement to round each of these numbers to the nearestinteger. For each number processed, print both the original number and the rounded number.arrow_forward(Count the letters in a string) Write a function that counts the number of letters in a string using the following header: def countLetters(s) : Write a test program that prompts the user to enter a string and displays the number of letters in the string. the answer should be in python.arrow_forward(Replace strings) Write the following function that replaces the occurrence of a substring old_substring with a new substring new_substring in the string s. The function returns true if string s is changed, and otherwise, it returns false. bool replace_strings (string& s, const string& old_string, const string& new_string) Write a test program that prompts the user to enter three strings, i.e., s, old string, and new_string, and display the replaced string.arrow_forward
- (Fraction calculator) | Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which _a_ and _b_ are integers and b ≠ 0. Your program must be menu driven, allowing the user to select the operation (+, –, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions: Function menu: This function informs the user about the program’s purpose, explains how to enter data, and allows the user to select the operation. Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.) Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the…arrow_forward(Rounding Numbers) Function floor may be used to round a number to a specific decimalplace. The statementy = floor(x * 10 + .5) / 10;rounds x to the tenths position (the first position to the right of the decimal point). The statementy = floor(x * 100 + .5) / 100;rounds x to the hundredths position (the second position to the right of the decimal point). Writea program that defines four functions to round a number x in various waysa) roundToInteger(number)b) roundToTenths(number)c) roundToHundreths(number)d) roundToThousandths(number)For each value read, your program should print the original value, the number rounded to thenearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth, and the number rounded to the nearest thousandth.arrow_forward(Bar-Chart Printing Program) One interesting application of computers is drawing graphsand bar charts. Write a program that reads five numbers (each between 1 and 30). For each numberread, your program should print a line containing that number of adjacent asterisks. For example,if your program reads the number seven, it should print *******.arrow_forward
- (Variable-Length Argument List: Calculating Products) Write a program that calculates theproduct of a series of integers that are passed to function product using a variable-length argumentlist. Test your function with several calls, each with a different number of arguments.arrow_forward4. (Check SSN) Write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid. Here are sample runs: Enter a SSN: 232-23-5435 JEnter 232-23-5435 is a valid social security number Enter a SSN: 23-23-5435 JEnter 23-23-5435 is an invalid social security numberarrow_forward(Python)Write a function that receives an integer. The function must return a string containing thehexadecimal representation of the integer.arrow_forward
- 4. (Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write a function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and prints all the prime numbers between 1 and 1000. Display 10 numbers per line.arrow_forward( MindTap - Cenage )Example 5-6 implements the Number Guessing Game program. If the guessed number is not correct, the program outputs a message indicating whether the guess is low or high. Modify the program as follows: Suppose that the variables num and guess are as declared in Example 5-6 and diff is an int variable. Let diff = the absolute value of (num - guess). If diff is 0, then guess is correct and the program outputs a message indicating that the user guessed the correct number. Suppose diff is not 0. Then the program outputs the message as follows: If diff is greater than or equal to 50, the program outputs the message indicating that the guess is very high (if guess is greater than num) or very low (if guess is less than num). If diff is greater than or equal to 30 and less than 50, the program outputs the message indicating that the guess is high (if guess is greater than num) or low (if guess is less than num). If diff is greater than or equal to 15 and less than 30, the…arrow_forward(Converting Strings to Integers for Calculations ) Write a program that inputs six strings that represent integers , converts the ngs to integers , and calculates the sum and average of the six values .arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education