In: Computer Science
C++ Please
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:
California College
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: