c - Displaying the total number of adjacent pairs in this program -
given input char[] find number of discrete pairs in string. input of:
"dddd"output 2 pairs"ddd"output 1 pair"dd"output 1 pair"d"output 0 pairs
the characters in each pair must adjacent. input of "abca" output still 0 because 'a's not adjacent.*
the goal find total number of pairs in string. input of "aaxbb" output should 2.*
for input string of char a[] = "dppccddd" there 3 pairs of adjacent letters my program's output 4. how solve problem?
int = 0, count = 0, count1 = 0; (i = 0; <= 6; i++) { if (a[i] == a[i + 1]) count++; } printf("%d", count);
just make code better, instead of hardcoding value of 6, use for(i = 0; < sizeof(a) / sizeof(a[0]) - 1; i++) number of elements in array.
the problem code if 2 chars matched, start comparing second 1 again, need skip character, increase i 1:
if(a[i] == a[i + 1]) { ++count; ++i; }
Comments
Post a Comment