Home > Backend Development > C++ > body text

Checks whether the characters of a string can become non-decreasing by replacing '_'

PHPz
Release: 2023-09-13 20:41:04
forward
498 people have browsed it

Checks whether the characters of a string can become non-decreasing by replacing _

In this article, we will delve into an interesting problem in the field of string manipulation: how to check whether the characters of a given string can become non- Descending order. This problem provides you with a great opportunity to practice your string manipulation and condition checking skills in C.

Problem Statement

Given a string consisting of alphabetic characters and question marks (?), determine whether the characters can be made non-decreasing by replacing the '?'s.

The non-decreasing condition means that for every two adjacent characters in the string, the ASCII value of the second character is not less than the ASCII value of the first one.

method

We will use a simple method to solve this problem −

  • Iterate through the string from left to right.

  • If a '?' is encountered, replace it with the character that came before it (unless it's the first character, in which case replace it with 'a').

  • Finally, check if the resultant string is non-decreasing.

Example

#include<bits/stdc++.h>
using namespace std;

bool checkNonDecreasing(string s) {
   int n = s.size();
   if (s[0] == '?') s[0] = 'a';
   for (int i = 1; i < n; i++) {
      if (s[i] == '?') s[i] = s[i-1];
      if (s[i] < s[i-1]) return false;
   }
   return true;
}
int main() {
   string s = "ac?b";
   bool result = checkNonDecreasing(s);
   if(result)
      cout << "Yes, the string can be made non-decreasing by replacing '?'s.\n";
   else
      cout << "No, the string cannot be made non-decreasing by replacing '?'s.\n";
   return 0;
}
Copy after login

Output

No, the string cannot be made non-decreasing by replacing '?'s.
Copy after login

The checkNonDecreasing function takes as input a string s and returns a boolean value indicating whether the characters of the string can be made non-decreasing by replacing '?'s.

In this test case, the input string is "ac?b". The checkNonDecreasing function is called with this string as the argument, and the result is a boolean value that is printed out.

in conclusion

Checking whether a character in a string can be made non-decreasing by replacing "?" is a question that tests your understanding of string operations and ASCII values. By practicing questions like this, you can strengthen your ability to work with strings in C.

The above is the detailed content of Checks whether the characters of a string can become non-decreasing by replacing '_'. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template