Check if the string contains consecutive letters and each letter occurs exactly once


Introduction

A string in C++ is a sequence of characters, which may be distinct or repetitive in nature. Consecutive characters are the characters occurring simultaneously, with a difference of 1. For instance, the characters a and b are consecutive, since they occur together. However, the characters m and o have a difference of 2 in their positions, which make them non-consecutive in nature.

In this article, we are going to develop a code that takes as input a string, and displays true in case all the characters in a string are consecutive. Let us look at the following example to understand the topic better

Sample Example

Example 1 − str − “pqsr”

Output − Yes

In this article, we will develop a code to extract the current and the previous character from the string. It is then further checked if the characters differ by position non-equivalent to 1, then the boolean false value is returned.

Syntax

sort()

sort(str.begin(), str.end())

The sort() method in C++ is used to arrange the characters in the string from the beginning to the end, in an increasing order.

Parameters

str - The input string

end - The last character occurring in the string

begin- The first character occurring in the string

length()

The length() method in C++ is used to compute the number of characters in the string.

str.length()

Parameters

str - The input string

Algorithm

  • An input string, str is accepted as input.

  • The input string is sorted using the sort() method.

  • An iteration of the string is performed, using the for loop i.

  • The length of the string is computed using the length() method and stored in len variable.

  • A for loop iteration, i is performed over the string.

  • Each time the character at ith, ch and i-1th, ch1 position is extracted.

  • If the difference between these two characters is not equal to 1, then a boolean false value is returned

  • If all the corresponding characters satisfy the required condition, then the boolean value - true is returned.

  • This value is returned in the form of a boolean flag, res. If its value is true, the string contains consecutive characters is printed

Example

The following C++ code snippet is used to take as input a sample string and compute whether all the characters occurring in the string are consecutive in nature −

//including the required libraries 
#include <bits/stdc++.h>
using namespace std;
 
//function to check of characters consecutive
bool validateString(string str) {
 
   //length of the string
   int len = str.length();
 
   // sorting the given string
   sort(str.begin(), str.end());
 
   // Iterate for every index and
   // check for the condition
   for (int i = 1; i < len; i++) {
 
      //extract characters at the required pos 
      char ch = str[i];
      char ch1 = str[i-1];
      if (ch-ch1 != 1)
      //in case characters are not consecutive
         return false;
   }
   //if condition holds
    return true;
}
 
//calling the main method
int main() {
 
   // 1st example
   string str = "mpon";
   cout << "Input String : " <<str << " \n";
   bool res = validateString(str);
   if (res)
       cout << "Yes, the string contains only consecutive characters\n";
   else
       cout << "No, the string doesn't contain only consecutive characters.\n";
   return 0;
}

Output

Input String − mpon 
Yes, the string contains only consecutive characters

Conclusion

The constantly occurring characters in a string are the letters which occur simultaneously. This can be achieved by sorting of the string from the start to end of the string. The characters at consecutive positions can be easily compared and checked they differ by how many positions. This can be used to capture information that whether the strings are sequential or not.

Updated on: 31-Jul-2023

611 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements