# [Help] Simple Array/Pointer Program

• 03-30-2007
sandwater
[Help] Simple Array/Pointer Program
Hi,

I am new to arrays and pointers. I am trying to write a simple C program where the user inputs a string of characters. The program has to find the character that repeats itself the most times consecutively in the string. If there are two characters that repeat the same number of times then the program should return the first one only.

I can only use the <stdio.h> library.
---------------------------------------------------------
e.g.
Input:

Output:
the character "d" repeats 4 times

---------------------------------------------------------
What I am thinking of doing is using a pointer to point to each element of the array string one at a time and compare it to the previous character and then increment the counter if it is the same, but I get stuck trying to think how will I count for other characters and how will I determine which count is the highest. Do I need two different counters? Can someone suggest a different/efficient approach to this problem.
---------------------------------------------------------

Here's my attempt so far:
Code:

```#include <stdio.h> int character_repeat(char x, char *y); // x corresponds to str, y corresponds to *ptr //------------------------------------------------------------------------------------- int main() { char str[80], *ptr;         printf("Enter a string (< 80 characters): ");         gets(str);         ptr = str; // points to 1st element in the array string         printf("String: %s \n", str); // prints original string         int character_repeat(char x, char *y);         printf("The character %c repeats %d times", character_repeat(str), count); return 0; } //------------------------------------------------------------------------------------- int character_repeat(char x, char *y) { int i = 0, count = 0;         while (*y != '\0') {                 if (x[i+1] == x[i])                 i++;                 count++; // Need something more or different   } return count; }```

Any help/suggestions would be great. :)

Thanks.
• 03-30-2007
Think about how you'd do this by hand with your example problem.

Might make a row for each letter, and then as you scanned down the letters, put a tally mark next to each letter.

A - II
b - III
c - I
d - IIII

like that, yeah?

You could simply do the same thing in your program. The column with letters could be a char array. The column on the right side, could be an int array.

char letters[10] and int tally[10]

Then since you've tallied them in order, your char's and tally numbers will also be in order.

Using a for loop, you could set maxTally to tally[0] and then test each value after that in the tally array and see if (tally[i] > maxTally), and if so, then maxTally = tally[i], yeah?

Just remember the two array's are really parallel arrays and connected logically together through their subscripts. So if maxTally is found in tally[4], then you know it corresponds to the letter at letter[4].

Hope that helps.

```int character_repeat(char x, char *y) { int i = 0, count = 0;         while (*y != '\0') {                 if (x[i+1] == x[i])                 i++;                 count++; // Need something more or different   } return count; }```