I am having a problem and hopefully you guys can help me out.. I need to count the frequency of a character in a string(option 5 in the program. I can do this part however i cannot get my program to count anything that is in uppercase. It only does lower case.. please help out if you can.

/* Name:            

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

   // Function to reverse order of the string//
   void ReverseStr ( char *userstring, int start, int end )   
    char tmp ;

    if ( start >= end )
    printf ( "\n%s\n", userstring );

   tmp = *(userstring + start);
   *(userstring + start) = *(userstring + end);
   *(userstring + end) = tmp ;

   ReverseStr (userstring, ++start, --end );
   // Function to change character case //
  void f(char *str) 
        *str = tolower(*str);  
    else if(islower(*str)) 
        *str = toupper(*str); 
int main(void)
    int num6=6, opt;    // setting variables 
    char userstring[0]; // setting the string for user input
    char sub[0];        // seeting sub sting
    char str[0];
    char *p,*strptr;
    int i=0;
    int length=0;
    int c = 0, num[256] = {0};
    int d = 0;
    int count;
    int upperCounter;
    int lowerCounter;
    printf("This program will let you input a string then change it in a couple of different ways.\n");
    printf("Feel free to make the string as large or as small as you would like.\n");
    printf("You will then be given several options to choose from.\n");
    printf("Each option will give you a chance to go back to the main\n");
    printf("menu unless you exit the program.\n");
    printf("Please Enter your String\n\n");  //asking user for string
    gets(userstring);                        //getting string from user
    printf("\n\nPlease select from one of the following operations:\n\n");
    printf("1. Display your string\n");
    printf("2. Enter a smaller string and check if it is in your frist string\n");
    printf("3. Display your string with the case toggled\n");
    printf("4. Display your string in reverse order\n");
    printf("5. Display the statics of your string\n");
    printf("6. Quit\n");
   while(num6=6)       //Setting While loop
    printf("\nSelect Option:  "); //asking user for option
    scanf("%d", &opt);  // Takes in the option
   if (opt==6)    
   return 0; 
    switch(opt)    // switch set up to look for the option variable
    /*  OPTION #1 */
    case 1: //if user choses option 1
       printf("Your String is:\n ");   //displaying string for user
    /* OPTION #2 */
    case 2: //if user chooses option 2
    printf("Please Enter your sub-string : \n\n");  
    scanf("%s",sub);//reads in a string
    strptr = userstring;
    while ((strptr = strstr(strptr, sub)) != NULL) // Setting while loop to check the whole string

     strptr++;    // going to the next character
     count++;     // counter

   printf("The sub string is found %d times.\n", count);  //Display count for user
    /* OPTION #3 */
    case 3: //if user chooses option 3
    strcpy(str, userstring); 

    /* OPTION #4 */
    case 4: //if user chooses option 4
    printf("Your String in Reverse is: \n");         //printing result
    ReverseStr(userstring,0,strlen(userstring)-1);  // sending and recieving function to reverse string  
    /* OPTION #5 */
    case 5: //if user choose option 5
    printf("The Satistics of your String are:\n");
    length= strlen(userstring);                         //getting lentgh of string
    printf("Length:                    %d\n", length);  
    while ( userstring[c] != '\0' )            //looping string 
    for ( c = 0 ; c < 28 ; c++ )            // counting character variable and printing in order
      if( num[c] != 0 )
      printf("%c occurs %d times in the entered string.\n",c+'a',num[c]);

    /* OPTION #6  will quit the program*/
    /* If there is an incorrect option chosen */
    default: printf("Invaild response. Please choose from one of the seven choices. Thank you!\n");
return 0;

Recommended Answers

All 3 Replies

Convert the character to lower case before using it as an index:

num[tolower(userstring[c]) - 'a']++;

Note that you'll need to include <ctype.h> for tolower().

Sorry i did not make it clear.. but i need to keep it in uppercase and show it in my answer for example


a was used 1 time
b was used 1 time
c was used 1 time
d was used 1 time
D was used 1 time

Actually, looking at your code more closely, you're handling all possible character values. There's no need to attempt converting the character to lower case:


Though you'll definitely need to reconsider how you're printing the frequencies. Instead of just the first 28, go with all 256. The bonus of this is you'll capture all characters and not just the latin alphabet.

Be a part of the DaniWeb community

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