0

Program i've wrote is calculating how many times each letter appears in a string. I want to change it that it will find the character that appears the great amount of times in a row i.e. for the string "aabbbcccca" i want to printf "c" (as there are four c in a row and only two a and three b).

How to change my program that it will do the things i want? I am looking for the solution that will be as simple as possible and I want to use the existing code as much as possible.

#include "stdafx.h"
#include "string.h"
#include "ctype.h"

int count_nonspace(const char* str)
{
    int count = 0;
    while (*str)
    {
        if (!isspace(*str++))
            count++;
    }
    return count;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int a[127];
    int i = 0, j = 0, count[127] = { 0 };

    char string[100] = "Hello world";
    for (i = 0; i < strlen(string); i++)
    {
        for (j = 33; j<127; j++)
        {
            if (string[i] == (j))
            {
                count[j]++;
            }
        }
    }
    for (j = 0; j< 127; j++)
    {
        if (count[j] > 0)
        if (j < ' ' + 1) 
            printf("\n%d -> %d", count[j], j);
        else
            printf("\n%d -> %c", count[j], char(j)); 
    }

}

My idea for changing the code is the following (posted only the changed part): but still results are not as anticipated, why is that?

for (i = 0; i < strlen(string); i++)
{
    for (j = 33; j<127; j++)
    {

        if (string[i] == (j))
        {

            count[j]++;
            if (string[i] == string[i + 1])
                count[j]++;
            else
                best[j] = count[j];
        }
    }
}
Krowskir
  • 105
  • 4
  • 1
    `for (j = 33; j<127; j++) if(string[i] == j) count[j]++;` can be rewritten to just `j = string[i]; if (32 < j && j < 127) count[j]++;` – M Oehm Jan 20 '15 at 20:57
  • _I want to use the existing code as much as possible_ You can use most of your code. After your first pass, you have a count of each character. Now find the character whose count has the greatest value with a simple method. – M Oehm Jan 20 '15 at 21:06
  • You're going to need two arrays: `count[127]` and `best[127]`. When the character changes, update the `best` array if needed. – user3386109 Jan 20 '15 at 21:07
  • @user3386109 could you elaborate further on your method, because still i do not know how to do this – Krowskir Jan 20 '15 at 21:14
  • Given the string "AACCCBBCA". At index 2, we have a change from 'A' to 'C' with count['A'] == 2, so set best['A'] = 2. At index 5, we have a change with count['C'] == 3, so set best['C'] = 3. Likewise for the B's. Index 8 is where things get interesting. There's a change from 'C' to 'A', but at that point count['C'] == 1 and best['C'] == 3, so keep best['C'] = 3. – user3386109 Jan 20 '15 at 21:21
  • could it be done only with adding if loop or do i need to add for loop as well? – Krowskir Jan 20 '15 at 21:30
  • You'll need another `for` loop at the end, to find the largest number in the `best` array. – user3386109 Jan 20 '15 at 21:58

1 Answers1

0
#include "stdafx.h"
#include "string.h"
#include "ctype.h"

int count_nonspace(const char* str)
{
    int count = 0;
    while (*str)
    {
        if (!isspace(*str++))
            count++;
    }
    return count;
}


int _tmain(int argc, _TCHAR* argv[])
{
    int a[127];
    int i = 0, j = 0, count[127] = { 0 };

    int cur_count = 1; /* Gets compared with value in count[] */
    char cur_char = '\0';
    char string[100] = "Hello world";
    for (i = 0; i < strlen(string); i++)
    {
        if(cur_char == string[i])
        {
            cur_count++;
        }
        else
        {
            if(32 < cur_char && cur_char < 127)
            {
                if(cur_count > count[cur_char])
                {
                    count[cur_char] = cur_count;
                }
            }
            cur_char = string[i];
            cur_count = 1;
            if(32 < cur_char && cur_char < 127)
            {
                if(!(count[cur_char]))
                {
                    count[cur_char] = cur_count;
                }
            }
        }
    }

    /* Find the most consecutive char and print it. */
    char max_char = '\0';
    int max_count = 0;
    for(j = 0; j < 127; j++)
    {
        if(max_count < count[j])
        {
            max_count = count[j];
            max_char = j;
        }
    }
    printf("%c\n", max_char);
}
Matthew
  • 16
  • 4
  • What will be the printf function, that will printf the element, with greatest number of apperances in a row, then? – Krowskir Jan 20 '15 at 22:01
  • @Krowskir Had to remove that last part. Now it should just print the maximum character. – Matthew Jan 20 '15 at 22:10
  • Thanks i did not notice that at first, you're a genius appreciate your help, thank you very much! – Krowskir Jan 20 '15 at 22:11
  • I have one more question how to change this program in a manner that it will operate on files not strings? – Krowskir Jan 20 '15 at 23:14
  • @Krowskir I would suggest reading a file into a string and then operating on the string. Here is a link to a question about how to do this: http://stackoverflow.com/questions/174531/easiest-way-to-get-files-contents-in-c – Matthew Jan 20 '15 at 23:21