Home > Backend Development > C++ > In C language, the maximum number of characters between any two identical characters in a string

In C language, the maximum number of characters between any two identical characters in a string

王林
Release: 2023-09-17 20:53:02
forward
1246 people have browsed it

In C language, the maximum number of characters between any two identical characters in a string

We get a string of letters. There will be at least two identical characters in the array. The task here is to find the maximum number of characters between any two identical characters. If there are no duplicates of any characters, -1 is returned.

Input - String str = "abcdba"

Output - The maximum number of characters between any two identical characters in the string- 4

Explanation - The only repeated characters are 'a' and 'b', and their index is -

1. 2‘a’ first index 0 last 5 , characters in between 5-0-1=4
2. ‘b’ first index 1 last 4 , characters in between 4-1-1=2
   Maximum character in between repeating alphabets : 4
Copy after login

Input - String str = "AbcAaBcbC"

Output - Maximum number of characters between any two identical characters in the string - 5

Explanation - Duplicate The characters are 'A', 'b', 'c', and their indexes are as follows:

1. ‘A’ first index 0 last 3 , characters in between 3-0-1=2
2. ‘b’ first index 1 last 7 , characters in between 7-1-1=5
3. ‘c’ first index 2 last 6 , characters in between 6-2-1=3
   Maximum character in between repeating alphabets : 5
Copy after login

Note − If the input string is "abcdefg", there are no duplicate characters, so the function will return -1.

The method used in the following program is as follows

  • We use a character array to store the string Str[]

  • function maxChars(char str[],int n) is used to calculate the maximum number of characters between any two repeated letters.

  • We initialize the variable maxC to -1.

  • Traverse the array from the beginning of the string in a for loop.

  • Iterate through the remaining characters in a nested for loop and search for duplicate characters (if str[i] == str[j]).

  • If true, the difference between characters is calculated by subtracting the index (temp = j - i - 1).

  • If this value is the maximum value found so far, store it in maxC.

  • After traversing the entire string, return maxC.

Example

Demonstration

#include <stdio.h>
#include <stdio.h>
#include <math.h>
int maxChars(char str[],int n){
   int size = n;
   int maxC = -1;
   for (int i = 0; i < n - 1; i++)
      for (int j = i + 1; j < n; j++)
         if (str[i] == str[j]){
            int temp=abs(j-i-1);
            maxC = maxC>temp?maxC:temp;
         }
   return maxC;
}
// Driver code
int main(){
   char Str[] = "AbcAaBcbC";
   printf("Maximum number of characters between any two same character in a string :%d",
   maxChars(Str,9) );
   return 0;
}
Copy after login

Output

If we run the above code, it will generate the following output−

Maximum number of characters between any two same character in a string : 5
Copy after login

The above is the detailed content of In C language, the maximum number of characters between any two identical characters in a string. For more information, please follow other related articles on the PHP Chinese website!

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