Question

In: Computer Science

C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...

C++ Please

  1. Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than 'X', and 'b' is greater than 'a'. However, if any of the strings: "AABC" or "abcA" are passed to the function, the function returns "false", because in the first one equal characters are found and in the second one the character 'A' is following the character 'c' where the ASCII code of 'A' is smaller than the ASCII code of 'c'.
  2. Write a program that repeatedly reads a single character from the keyboard and if the character is a digit, the character is appended to a string that is initially null, otherwise, it won't be appended and the program continues. Once the user enters a '#' character, the input step terminates. After the input step terminates, the program calls the "isAscending" function defined in the previous step to check if the user entered the digits in ascending order or not, and displays one of the following messages accordingly:
    • "All digits were entered in ascending order."
    • "Digits were not entered in ascending order."

Two sample runs of this program are given below:

Sample Run 1:

Enter a sequence of characters and enter '#' to terminate the input:

A

B

&

0

3

j

)

9

x

#

All digits were entered in ascending order.

Sample Run 2:

Enter a sequence of characters and enter '#' to terminate the input:

A

B

&

0

3

j

)

2

x

#

Digits were not entered in ascending order.

Requirements:

  1. Use the "string" class to write this program.
  2. Except for the methods "length", "at", and "push_back" do not use any other method of the "string" class or any other library function in this program. The "push_back" is called on a string object and accepts a character as its input parameter and appends the character to the end of the string.

California College

Solutions

Expert Solution

Code:

#include <bits/stdc++.h> // importing library files for input/output/string functions
using namespace std;

// defining the function required
void isAscending(string s){
    int n=s.length(); // getting the length of string
    int count=1;
    for(int i=1;i<n-1;i++){
        if(s[i-1]>s[i]){  // condition checking the order
            break;
        }
        count++;
    }
    if(count==n-1){  // printing based on the sequence
        cout<<"All digits were entered in ascending order."<<endl;
    }else{
        cout<<"Digits were not entered in ascending order."<<endl;
    }
}

// driver code
int main() {
        char c;
        string s;
        while(c!='#'){  // taking inputs
            cin>>c;
            s.push_back(c);
        }
        isAscending(s);  // calling the function
        return 0;
}

Output:


Related Solutions

Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an object of the type "string" and "ch" is a character value with the default argument of 'A'. The function looks for the character "ch" in the string  "s" and displays all the indices of its occurrences on the screen. For example, if the caller sends the values of "ABCDBGAB" and 'B' to the function for the parameters "s" and "ch", respectively, the function displays the...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an...
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an integer number. The function, which must be a value-returning function, returns 1 if the number is even or 0 if the number is odd. In the “main” function (i.e. def main()), capture the return value and print an appropriate message on screen (i.e. number is even or odd). Rubric: Correctly defined a value-returning function: 5 pts Correctly capture and use return value from a...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer...
C++ Only Create a function named PrintStudents, which takes a string input filename and an integer minimum score value and a string output file name as a parameters. The function will read the student scores and names from the file and output the names of the students with scores greater than or equal to the value given. This function returns the integer number of entries read from the file. If the input file cannot be opened, return -1 and do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT