C countLetter function

c program to count number of characters in a string
cs50 list of functions
how to count letters in c
c++ count letters in word
count letters cs50
readability cs50 solution
ctype h cs50
getstring cs50

So I have created a function which currently counts all individual lower case letters in a string, and outputs them like:

a : 3

b : 5

and so on...

I am struggling to get the function to add both the uppercase and lowercase letters together, so that there is one output for both values. For example, if there is 3 'a' and 4 'A', then the output for A / a should be 7. At the moment only the lowercase characters are being output.

#include <stdio.h>

void countLetters(char * strings[])
{
int loweralphabet[26] = { 0 };
int upperalphabet[26] = { 0 };
int i, j;

for (i = 0; i <= 2; i++) {
    for (j = 0; strings[i][j] != '\0'; j++) {
        char c = tolower(strings[i][j]);
        if (c >= 'a' && c <= 'z') {
            loweralphabet[c - 'a']++;
        if (c >= 'A' && c <= 'Z') {
            upperalphabet[c - 'A']++;
        }
        }
    }
}
printf("\n");

for (i = 0; i < 26; i++) {
    printf("%c / %c: %d\n", ('a' + i), ('A' + i), loweralphabet[i], 
upperalphabet[i]);
}
printf("\n");

return 0;
}

int main()
{
char * strings[] = { "Is laid back living your thing, ",
                     "or are you an adrenaline junkie always seeking 
                      adventure ?",
                     "Are you a culture lover looking to learn new things or 
                      do you live for the night ?",
                     "Do your friends see you as a sports fanatic, ",
                     "or are you a frequent gig goer obsessed with music ? 
                     ",
                     "Whichever of these you identify with, x is the 
                      place where you can follow your interests ",
                     "as well as explore new passions.",
                     "" };
countLetters(strings);
}

If I just add the two counts together in the print function, when outputting the program this shows:

        char c = tolower(strings[i][j]);
        if (c >= 'a' && c <= 'z') {
            loweralphabet[c - 'a']++;
        if (c >= 'A' && c <= 'Z') {

After line #1, condition in line #4 will never be satisfied. Moreover, it will never be satisfied due to the fact that it is placed inside the then-block of condition in line #2 (i.e. you have made sure that c is lowercase, and now you check if it is uppercase; of course it's not).

In fact, it is not quite clear what are you struggling with. As your program is written now, it should count letters case-insensitive.

Solved: (Count the letters in a string) Write a function that c, (Count the letters in a string) Write a function that counts the number of letters in a C-string using the following header: int countLetters(const char s[]). Write a test  Write a C program to count number of characters, words and lines in a text file. Logic to count characters words and lines in a file in C program. How to count total characters, words and lines in a text file in C programming.

Figured it out. The program currently is only counting the first 3 strings:

    for (i = 0; i <= 2; i++)

After asking it to count all 7, the program functioned as intended.

Insight Through Computing: A MATLAB Introduction to Computational , that dna contains only the letters 'A', 'T', 'C', and 'G'. P9.1.7 Complete the following function so that it performs as specified: function k = FindSubstring(S1,S2)  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more . How are we doing? Take our short survey. Function to count all characters in a string - C++. Ask Question Asked 6 years, 10 months a

here is the corrected counting loop

for (i = 0; i <= 2; i++) {
    for (j = 0; strings[i][j] != '\0'; j++) {
        char c = strings[i][j];
        if (c >= 'a' && c <= 'z') 
            loweralphabet[c - 'a']++;
        if (c >= 'A' && c <= 'Z') 
            upperalphabet[c - 'A']++;


    }
}

you were setting c to lowercase and then trying to test if it was upper case, but your test nesting was wroing too

YOur code actually says this once the indentation is fixed.

for (i = 0; i <= 2; i++) {
    for (j = 0; strings[i][j] != '\0'; j++) {
        char c = tolower(strings[i][j]);
        if (c >= 'a' && c <= 'z') {
            loweralphabet[c - 'a']++; <<<< missing } here
            if (c >= 'A' && c <= 'Z') { <<< can never be true c is always lower case , even if you got here
                upperalphabet[c - 'A']++;
            }
        }
    }
}

so you were never doing the second test

Programming for Computations, Call the function to determine the frequency of C and G in the gene string text.​count(letter)/len(text); b) text.count(letter*2); c) len(re.findall('G[AT]+?GG', text)). The integer entered by the user is stored in variable n.Then the while loop is iterated until the test expression n! = 0 is evaluated to 0 (false).. After the first iteration, the value of n will be 345 and the count is incremented to 1.

C++Write A Function With The Name CountLetter That, Answer to C++Write a function with the name countLetter that receives as input-​parameters: an array of strings, an int containing As you see you can use a function to encapsulate the code for you so you won't have to write out the for loop everytime, but can just use count_underscores("my_string_") in the rest of your code. Using advanced C++ algorithms is certainly possible here, but I think it's overkill.

C++ for Programmers, 3 // Member functions are defined in GradeBook.cpp 4 5 #include <string> 5.10 GradeBook class uses a switch statement to count letter grades A, B, C, D and  A function can also be referred as a method or a sub-routine or a procedure, etc. Defining a Function. The general form of a function definition in C programming language is as follows − return_type function_name( parameter list ) { body of the function } A function definition in C programming consists of a function header and a function body

Counting Letters, if c == 'a': 5. count = count + 1. 6. return count. 7. ​. 8. print(countA("banana")) In order to do this we need to realize that writing a function that returns a single  representing the number of characters in the test string. Use a cout statement in main() to print out the length of the string. Please note: Your function should count the characters in the string, not access some C++ library function or method. DO NOT #include <string.h> or <string>.

Comments
  • In the printf just add the two counts together with a + sign.
  • Not sure if it's a copy/paste mistake but there is probably an error with your curly parentheses in the section where two if statements are.
  • The output you showed us almost certainly didn't come from the code you showed us. if you want us to fix the bug in some code, you need to show us the code with the bug -- not some other code that works.
  • I said that the displayed output is shown when I try to add the two count functions together in the printf function. My goal is for the program to count each letter of the alphabet in the strings listed below, with it being case insensitive. Currently the program is either counting only lower case characters, or only some. I updated my post to display all the code in the program @DavidSchwartz
  • for a start this code doesnt compile. Its missing ctype.h and your printf has 3 '%' but 4 args passed in. YOu return 0 from a function declared void
  • Only lowercase letters are being counted right now, which is why I too am confused.
  • @JoshB Can you be more precise about what you mean by 'only lowercase letters are being counted'? Are uppercase letters being ignored? Are you talking about what the code you showed in the question or some other code?