
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count characters with same neighbors in C++
We are given a string with, let’s say, str and the task is to compute the count of characters in a string str having the same neighbors and that will include both left and right side of a character in a string. Also, in this scenario the first and last character in a string will always be considered as they have only one adjacent character.
For Example
Input − string str = “poiot” Output − count is 3
Explanation − In the given string characters p, t and i are having the same neighbors so the count will be increased to 3.
Input − string str = “nitihig” Output − count is 4
Explanation − In the given string characters n, t, h and g are having the same neighbors so the count will be increased to 4.
Approach used in the below program is as follows
Input the string in a variable let’s say str
Calculate the length of string str using length() function that will return an integer value as per the number of letters in the string including the spaces.
If the length of the string is less than or equals 2 then return the count as the length of the string because they all will be counted.
If the length of the string is greater than 2, initialise the count with 2.
Now, start the loop with i to 1 till i is less than (length-1)
Inside the loop, check if (str[i-1] = str[i+1]) then increase the count by 1
Now return the total value of count
Print the result.
Example
#include <iostream> using namespace std; // To count the characters int countChar(string st){ int size = st.length(); if (size <= 2){ return size; } int result = 2; // Traverse the string for (int i = 1; i < size - 1; i++){ // Increment the count by 1 if the previous // and next character is same if (st[i - 1] == st[i + 1]){ result++; } } // Return result return result; } int main(){ string st = "poiot"; cout <<"count is " <<countChar(st); return 0; }
Output
If we run the above code it will generate the following output
count is 3
- Related Articles
- Count substrings with same first and last characters in C++
- Count characters at same position as in English alphabets in C++
- Maximum count of substrings of length K consisting of same characters in C++
- Count number of substrings with exactly k distinct characters in C++
- Count numbers with same first and last digits in C++
- Count subarrays with same even and odd elements in C++
- Count pairs with average present in the same array in C++
- JavaScript Count characters case insensitive
- Placing same string characters apart in JavaScript
- Count common characters in two strings in C++
- Check if a string has all characters with same frequency with one variation allowed in Python
- Fill array with 1's using minimum iterations of filling neighbors in C++
- Python – Replacing by Greatest Neighbors in a List
- MySQL query to count frequency of students with the same age?
- Merge JavaScript objects with the same key value and count them
