I am having troubles with a program I wrote to count letters in a string. Here is the program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
int length;
int ascii;
char cipher[3];
int letters[26];

int main()
{

    char cipher[10] = "ABC"; /*Or an inputted variable, but that's not important at the moment.*/
    length = strlen(cipher);
   
    for (i=0; i<length; i++)
        {
              ascii = cipher[i];
             
              if (ascii > 96)
                 ascii = ascii - 97;
              else
                  ascii = ascii - 65;
              
              letters[ascii]++;
            
              
        }
        
        printf("A: %d   ", letters[0]);
        printf("B: %d   ", letters[1]);
        printf("C: %d   ", letters[2]);
        printf("D: %d\n", letters[3]);
        printf("E: %d   ", letters[4]);
        printf("F: %d   ", letters[5]);
        printf("G: %d   ", letters[6]);
        printf("H: %d\n", letters[7]);
        printf("I: %d   ", letters[8]);
        printf("J: %d   ", letters[9]);
        printf("K: %d   ", letters[10]);
        printf("L: %d\n", letters[11]);
        printf("M: %d   ", letters[12]);
        printf("N: %d   ", letters[13]);
        printf("O: %d   ", letters[14]);
        printf("P: %d\n", letters[15]);
        printf("Q: %d   ", letters[16]);
        printf("R: %d   ", letters[17]);
        printf("S: %d   ", letters[18]);
        printf("T: %d\n", letters[19]);
        printf("U: %d   ", letters[20]);
        printf("V: %d   ", letters[21]);
        printf("W: %d   ", letters[22]);
        printf("X: %d\n", letters[23]);
        printf("Y: %d   ", letters[24]);
        printf("Z: %d   ", letters[25]);
      
    getchar();
    return 0;
}

This runs fine, and I'm quite happy with it. However, I am trying to integrate it into a larger program, with a GUI. This uses the windows.h header. Obviously, this program outputs a large amount of variables. However, I want it to output to an edit control. To do this, I somehow need to turn the int into a char of THAT INT, not the ascii character for that value. Does anyone know how to do this?

>>To do this, I somehow need to turn the int into a char of THAT INT, not the ascii character for that value.
Do you mean if you have an int with the value 15 then you want a string "15" so that you can display it in an edit control ? You can use sprintf() to do that, like this:

char buf[26];
int n = 15;
sprintf(buf,"%d", n);

Now after the above is executed you can send the edit control a message what displays the text in buf variable.

Yes, that's exactly what I mean. I'll have a go at that now.

BTW lines 31 - 56 could be coded in just three lines of code, which includes a short loop.

Yes, but how would you get the letters?

Yes, but how would you get the letters?

for ( i = 0; i < 25; i++ )
        {
            printf( "%c: %d", 'A' + i, letters[i] );
            if ( ( i + 1 ) % 4 == 0  ) {
                putchar( '\n' );
            }
            else {
                putchar( '\t' );
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.