Thread: Again Character Count, Word Count and String Search

  1. #1
    Registered User client's Avatar
    Join Date
    May 2002
    Posts
    12

    Unhappy Again Character Count, Word Count and String Search

    Thanks for the responses but that was not exactly what I was looking for.
    First of all I use Borland C++ 3.1 and this program is running under DOS.

    So I'll write again my problem....

    Program Starts
    1st String is defined as
    input01[255]
    2nd String is defined as
    input02[255]

    then the program asks to enter the 1st String then the Second string.

    Afterwards a menu appears, which works with switch.

    So here are my questions:
    1. If you enter the 1st Menu point, you'll be asked for which character to search.

    i.g.:
    Enter Char to Search: 'a'

    then this character is counted in the String. For example the first String was. "A Apple a day keeps the doctor away" Second string was "Another day in paradise".

    Then something like this should appear

    First String : 6
    Second String: 4

    I've tried this many times with assigning a pointer but it didn't work.
    Also upper and lowercase should be considered.

    2nd Question: How can I count the words, I've tried it several times with 'strtok' but had no success. So if you enter this menu. Something like this should appear. Consider again the Strings above given.

    First String: 8
    Second String: 4

    3rd and last question:
    If you enter the 3rd Menu you'll be asked for to enter a word to be searched. For example this should work like this. again conisdering the strings above given.

    Enter Word to Search: Apple

    1st String: Found
    2nd String: Not Found

    I've tried this also with strstr but it didn't work.

    As an example the program will look like this.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {  
       char input01[255], input02[255], char01, string01;  
       int i = 0, choice;  
    
       printf("\nEnter First String: ");  
       gets(input01);  
       printf("Enter Second String : ");  
       gets(input02);  
    
       while(i == 0)       
               {        
                  printf("Please Choose\n");        
                  printf("1. Count Character\n");        
                  printf("2. Word Count\n");        
                  printf("3. Search for a Word\n");        
                  printf("4. Exit\n");       
                  printf("Your choice: ");        
                  scanf("%d", &choice);        
    
                  switch(choice)              
                            {               
                                case 1 : clrscr();                        
                                              printf("Enter the Char to Search: ");
                                              scanf("%c", char01);
                                              ????????
                                              break;               
                                case 2 :  clrscr();
                                               printf("WORD COUNT\n\n"); 
                                               ????????? 
                                               break;               
                                case 3 :  printf("Enter Word to Search in String: ");                        
                                               gets(string01);                        
                                               ?????????
                                               break;               
                                               case 4 : i = 1;                        
                                               break;             
                            }
    }
    It should be something like this, so I actually don't need the creme-a-la-creme of the codes, just a simples one do this functions.


    with my best reagrds... Special Thanks to Lynux_Penguen and Hammer

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Easy one to get you going

    Code:
    int count_char_in_string ( char *str, char ch ) {
      int i, count = 0;
      for ( i = 0 ; i < strlen(str) ; i++ ) {
        if ( str[i] == ch ) count++;
      }
      return count;
    }
    Which you call like so
    Code:
        case 1 : clrscr();                        
            printf("Enter the Char to Search: ");
            scanf("%c", &char01);
            printf( "First String : %d\n",  count_char_in_string( input01, char01 ) );
            printf( "Second String : %d\n",  count_char_in_string( input02, char01 ) );
            break;

  3. #3
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59
    that was not easy
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM