Number of occurence of each alphabet in a user inputted string.

Firstly I am a first timer noob at programming so a sample program or the below program with your edits and tweaks would be awesomely helpful. Thank you.
I want to make a program that prompts an input(a string) and then returns or prints out the number of times a letter and a space occurs in the string FOR EVERY LETTER OF THE ALPHABET. Here is my code which is obviously not working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <ctype.h>
#define SIZE 20

int main(void)
{
	char sentence[SIZE];
	int i;
	int Alphabet[26];
	int count = 0;

	char Alphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','r','s','t','u','v','w','x','y','z','\0'};

	printf("Enter a line of text: ");
	if (fgets(sentence, sizeof sentence, stdin)){
		printf(sentence);
	}
	for(i=0; sentence[i] != '\0'; i++){
		sentence[i] = tolower(sentence[i]);
	}
	printf(sentence);
	getchar();

	for (i=0; i<=26; ++i){
		if (Alphabet[i]==sentence[i]){
			count++;
		}
	}

	return count;
	
}



(And this is the error it gives when debugging just in case you needed it)

1>------ Build started: Project: Project 5, Configuration: Debug Win32 ------
1>  Project_5.c
1>c:\users\howard\documents\visual studio 2010\projects\project 5\project 5\project_5.c(12): error C2371: 'Alphabet' : redefinition; different basic types
1>          c:\users\howard\documents\visual studio 2010\projects\project 5\project 5\project_5.c(9) : see declaration of 'Alphabet'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Please do help me, i really appreciate!
I hope you realize that what you are writing isn't C++, but C.

1
2
3
4
	int Alphabet[26];
	int count = 0;

	char Alphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','r','s','t','u','v','w','x','y','z','\0'};


you're declaring Alphabet twice get rid of the first one.
Zephilinox has identified the cause of the error. (and that the code isn't C++).

However, an array of integers is probably needed, in order to maintain separate counts for each letter (and space!).

The for loop at line 24 probably needs to be modified in order to increment the appropriate separate count. In fact not just modified, but considerably enhanced/changed.

And then, you need to do something with the results (print out?).

Last edited on
what's the difference between c and c++ and thanks for the advice Zephilinox but how do i modify the loop to get what i'm asking for?
1
2
#include <stdio.h>
#include <ctype.h> 


these are c headers, the C++ version of these are

#include <cstdio>
#include <cctype>


I believe its the same code, but wrapped in the std namespace.

printf(sentence);
also C, as are many of the other functions you used.

Basically, your code is C if it compiles in C compiler, and your code is C++ if it doesn't (but most of C can be compiled in a C++ compiler)

because I don't know C, I can't really fix your code without looking through C documentation, but this is how I would do it in C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <iostream>
#include <cstdio>

int main()
{
    //setup
    std::vector<int> alphabetCount;

    for (int i = 0; i < 26; ++i)
    {
        alphabetCount.push_back(0);
    }

    //now the interactive bit
    std::cout << "Enter a line of text\n";
    std::string line;
    std::getline(std::cin, line);

    for (size_t i = 0; i < line.size(); ++i)
    {
        char currentChar = tolower(line[i]);
        if (isalpha(currentChar))
        {
            ++alphabetCount[currentChar - 'a']; //subtract a, so if currentChar = a, 'a' - 'a' = 0, so its index 0
        }
    }

    for (size_t i = 0; i < alphabetCount.size(); ++i)
    {
        std::cout << "there were " << alphabetCount[i] << " occurences of " << static_cast<char>(i + 'a') << "\n"; //add 'a' for the same reason as above, though we have to cast it to a char.
    }

    return 0;
}

Last edited on
Thanks but it doesn't still give me the occurence of the rest of the alphabets and the spaces
Topic archived. No new replies allowed.