Checking if it's letter digit or special character

Hey guys i need some help, i started a program which checks in file for letters, digits and special characters but it seems i have some mistake because it only prints 0... Any help would be much appreciated! Here's my code:

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
36
37
38
39
40
41
42
43
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define MAX_SIZE 100
int main()
{
    ifstream myFile;
    char string[MAX_SIZE];
    int alphabets;
    int digits;
    int specialchars;
    int i;

    alphabets = digits = specialchars = i = 0;

    myFile.open("text.txt");

    while (myFile >> string){

        while (string[i]!='\0'){

            if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')){
               alphabets++;
               }
            else if(string[i]>='0' && string[i]<='9'){
                digits++;
            }
            else {
                specialchars++;
            }
            i++;
        }

    }
cout<<"Alphabets: "<<alphabets<<endl;
cout<<"Digits: "<<digits<<endl;
cout<<"Special characters: "<<specialchars<<endl;

    myFile.close();
    return 0;
}
Why do you include string if your not going to use it? How about you make char string[max_size] be std::string str; and then use getline to limite the amount of chars read? (std::getline(std::cin,str,100);)
you need to reset `i' for each word read
Registered users can post here. Sign in or register to post.