Chars

This is a discussion on Chars within the C Programming forums, part of the General Programming Boards category; My program: Code: int main() { char string[100]; int i=0, j=0, NoLetter[25]; clrscr(); printf("Enter string: "); scanf("%s", string); do { ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Chars

    My program:

    Code:
    int main()
    {
       char string[100];
       int i=0, j=0, NoLetter[25];
    
       clrscr();
       printf("Enter string: ");
       scanf("%s", string);
       do
       {
          for (j=97; j<=122; j++)
          {
             if (string[i] == j)
             {
                NoLetter[j-97]++;
                printf("\nString contains %d \"%c\"", NoLetter[j-97], string[i]);
             }
          }
       } while (string[i++] != '\0');
       getch();
       return 0;
    }
    The problem is the output:

    Enter string: hello

    String contains 1 "h"
    String contains 1 "e"
    String contains 1 "l" <- this I do not want to print it on the screen
    String contains 2 "l"
    String contains 1 "o"

    I know that this prints out beacuse it is in the loop. I tried various ways, but none worked out.

    Please help.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    How about
    Code:
    #include <stdio.h>
    
    int main()
    {
        char string[100];
        int i=0, j=0, NoLetter[26] = { 0 };  // in my alphabet at least
    
        printf("Enter string: ");
        scanf("%s", string);
    
        for ( i = 0 ; string[i] != '\0' ; i++ ) {
            NoLetter[ string[i] - 'a' ]++;
        }
    
        for ( j = 'a' ; j <= 'z' ; j++ ) {
            if ( NoLetter[j-'a'] != 0 ) {
                printf("\nString contains %d \"%c\"", NoLetter[j-'a'], j );
            }
        }
        return 0;
    }

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thank you Salem.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

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