[Help] Simple Array/Pointer Program

This is a discussion on [Help] Simple Array/Pointer Program within the C Programming forums, part of the General Programming Boards category; Hi, I am new to arrays and pointers. I am trying to write a simple C program where the user ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    1

    Question [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:
    enter a string: AddCCCddddeeeedddCCA

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    Adak

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You really don't need main()'s variable ptr. It's not used for anything useful.

    Code:
    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;
    
    }
    As written, that is an infinite loop (assuming y[0] != 0). You need to increment y.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21