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...
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...
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
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write A function with an input parameter which is a string arithmetic statement which contains only...
Write A function with an input parameter which is a string arithmetic statement which contains only alphabet variables and binary operations including +, -, *, / and % and checks whether the statement is a valid arithmetic statement or not. If the statement is valid, the function returns true, otherwise returns false. • The statement might contain parenthesis as well. For instance: • a+b*a+c/c%y • (a+b)*(a/d-(a/b)) • You can make this assumption that the variable names contain only one alphabet...
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT