Concept explainers
Explanation of Solution
Use of pointers to pass C-string argument:
In C++, for passing C-string argument the pointers are extremely useful. Each letter is passed through the pointer parameter variable. It will read until the null character, are part of the string.
Example program:
Consider the example that defines a function that uses a pointer to count the number of times a particular letter appears in a C-string is as follows:
//Header files
#include<iostream>
using namespace std;
//function prototype
int count(char*, char c);
// Define Main function
int main()
{
//Declare the constant variable
const int S = 20;
// Declare the variables as char type
char str[S], l;
//Get the input string from the user
cout << "Enter the string: ";
cin.getline(str, S);
//Get the letter from the user
cout << "Enter a letter: ";
cin >> l;
//call the function
cout << "The total number of times the " << ...
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
MyProgrammingLab - For Gaddis: Starting Out with C++ From Control Structures through Objects
- (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays so that a customer’s record can be accessed randomly by account number. Create the file by entering five customer records, with each record consisting of an integer account number (starting with account number 1000), a first name (maximum of 10 characters), a last name (maximum of 15 characters), and a double-precision number for the account balance. After the file is created, write a C++ program that requests a user-input account number and displays the corresponding name and account balance from the file.arrow_forward(Data processing) Write a C++ program that reads the file created in Exercise 4, permits the user to change the hourly wage or years for any employee, and creates a new updated file.arrow_forwardC Program Pass-by-reference in Functions Using Pointers Write a function swap that swaps the values of three integers x,y, and z. x is swapped to y, then y to z, and lastly z to x. In the main function, write a program that ask for theee integer inputs and pass in the reference of those variables by calling swap function. more details below the photo.arrow_forward
- Write a C code that finds the numbers of the Fibonacci sequence in a sequence entered by the user. Note that dynamic memory functions must be used if arrays are to be used.arrow_forwardwrite in c++ please but not use stl, use array please... Use the following headers to create a function that returns sorted strings. Create a test program that receives the string sort(string&s) string and outputs the newly sorted string to the screen.sample execution resultEnter a string s : silentThe sorted string is eilnstarrow_forwardIn c++ Use pointer. Write a function named “hasHelpOption”that accepts argc and argv (same data typesof command line arguments), it will return true if it has the argument of “/help”or “/?”or “—help”and false otherwise.Please show how this function is being used and tested.arrow_forward
- TOPIC: POINTERS 1.Create a C function main() and do the following sequence of instructions:1. Declare two integer variables x and y.2. Declare two pointer variables p and q.3. Assign the following values: x=15, y=25.4. Print the values of x and y.5. Let the pointer p points to the address of x6. Print the value of x and the address of x using the pointer variable p7. Increment the value of x by 5 using the pointer variable p8. Print the value of x (using x and p variables)9. Let the pointer variable q points to the address of y.10. Print the value of y and the address of y using the pointer variable q.11. Add the value of x to the value stored in y using their pointer variables p and q.12. Print the new values of x and y using the pointer variables p and q.13. Let pointer p points to the address where the pointer q also points.14. Print the address of the variable pointed to by p and q.15. Change the value of y (let y=50) using the pointer p16. Print the value of x and y (using their…arrow_forward(C++) 9. True/False: the strcpy() function will make sure there is enough memory allocated in the destination string before copying C-strings 10. True/False: when creating a string object, you must dynamically allocate enough bytes to hold the string 11. Consider the following statement, assuming goAgain is a valid char. Rewrite it using toupper() or tolower() if (goAgain == 'y' || goAgain == 'Y') 12. Write a C++ function which accepts a pointer to a C-string as its argument. It should return the number of words in the C-string. For example, for the C-string “The Giants won the pennant!” your function should return 5. You may assume the parameter passed is a pointer to a valid, null-terminated C-string with no newlines or tabs, exactly one space separates each word, and there is at least one word. int wordCounter(char* str)arrow_forwardIn C++, Complete the following function that takes an integer as an argument and returns a power of two greater than or equal to input. If input is less than 1, the function should return 0. Ex.) 1.) 8 => 8. 2.) 15 => 16. 3.) 17 => 32 My code: #include <bits/stdc++.h> #include <fstream> #include <iosstream> #include <sstream> #include <strings> /* complete the 'PowerOfTwoCeling */ int PowerOfTwoCeling(int a) { } int main() { std::ofstream fout(std:: getenv("OUTPUT_PATH")); std::string inputString; std::getLine(std::cin, inputString); std::stringstream input(inputString int a; input >> a; fout<< PowerOfTwoCeling(a); fout.close(); return 0; }arrow_forward
- IN C++, TRUE OR FALSE : The statement delete p; deallocates the dynamic array that is pointed to by p. In the statement cin >> y;, y can only be an int or double variable. In an output statement, the newline character may be part of the string. The extraction operator (>>) skips whitespace characters when searching for the next data in the input stream.arrow_forwardProblem statement def stringMatch(subStr,strLine): """ This function returns the index of the first occurence, first character, of the <subStr> in the <strLine> Input: - subStr: the substring we would like to search for - strLine: the line in which we would like to search for <subStr> Output: - index: the index of the first occurence, first character, of the <subStr> in the <strLine> """ return 0arrow_forwardMost C++ programs that do I/O should include the___________ header that contains thedeclarations required for all stream-I/O operations.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning