Alphabet Adjacent Pairs in Sequence ID:5392


 Accept a string S which contains only lower case alphabets and print the count of instances C where in a pair of adjacent characters, the right character is next to the left character in the original alphabetical sequence.

Boundary Condition(s):
1 <= Length of S <= 1000

Input Format:
The first line contains S.

Output Format:
The first line contains the integer value C

Example Input/Output 1:
Input:
abegh

Output:
2

Explanation:
ab gh are the two instances.

Example Input/Output 2:
Input:
abcdef

Output:
5

Explanation:
ab bc cd de ef are the five instances.

Program in C:
#include<stdio.h>
#include <stdlib.h>

int main()
 {
char s[1002];
scanf("%s",s);
int ac=0;
int an = strlen(s);
for(int i=0;i<an-1;i++)
{
  if((s[i+1]-s[i])==1){
    ac+=1;
  }
}
printf("%d",ac);
}

Comments

Popular Posts