A long long code ! Please see this.

Is there any short way to write this program ?
Please note that we cannot use any string related functions.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
       using namespace std;

int main()
{
    int freq(char abc[], int len), kam; char che[999];
    cout<<"Enter the string you wish to process.\n";
    cin.getline(che, 999);
    for(int kam=0; che[kam]!='\0'; kam++);
    freq(che, kam); return 0;
}

int freq(char abc[], int ram)
{
    char poo; int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0;
    int m=0, n=0, o=0, p=0, q=0, r=0, s=0, t=0, u=0, v=0, w=0, x=0, y=0, z=0;
    for(int ham=0; abc[ham]!='\0'; ham++)
    {
        poo=abc[ham];
        if((poo=='a')||(poo=='A'))
        {a++;}
        if((poo=='b')||(poo=='B'))
        {b++;}
        if((poo=='c')||(poo=='C'))
        {c++;}
        if((poo=='d')||(poo=='d'))
        {d++;}
        if((poo=='e')||(poo=='E'))
        {e++;}
        if((poo=='f')||(poo=='F'))
        {f++;}
        if((poo=='g')||(poo=='G'))
        {g++;}
        if((poo=='h')||(poo=='H'))
        {h++;}
        if((poo=='i')||(poo=='I'))
        {i++;}
        if((poo=='j')||(poo=='J'))
        {j++;}
        if((poo=='k')||(poo=='K'))
        {k++;}
        if((poo=='l')||(poo=='L'))
        {l++;}
        if((poo=='m')||(poo=='M'))
        {m++;}
        if((poo=='n')||(poo=='N'))
        {n++;}
        if((poo=='o')||(poo=='O'))
        {o++;}
        if((poo=='p')||(poo=='P'))
        {p++;}
        if((poo=='q')||(poo=='Q'))
        {q++;}
        if((poo=='r')||(poo=='R'))
        {r++;}
        if((poo=='s')||(poo=='S'))
        {s++;}
        if((poo=='t')||(poo=='T'))
        {t++;}
        if((poo=='u')||(poo=='U'))
        {u++;}
        if((poo=='v')||(poo=='V'))
        {v++;}
        if((poo=='w')||(poo=='W'))
        {w++;}
        if((poo=='x')||(poo=='X'))
        {x++;}
        if((poo=='y')||(poo=='Y'))
        {y++;}
        if((poo=='z')||(poo=='Z'))
        {z++;}
    }
    cout<<"\nThe frequency of each alphabet in the given string is:\n";
    if(a!=0)
    {cout<<"a= "<<a<<endl;}
    if(b!=0)
    {cout<<"b= "<<b<<endl;}
    if(c!=0)
    {cout<<"c= "<<c<<endl;}
    if(d!=0)
    {cout<<"d= "<<d<<endl;}
    if(e!=0)
    {cout<<"e= "<<e<<endl;}
    if(f!=0)
    {cout<<"f= "<<f<<endl;}
    if(g!=0)
    {cout<<"g= "<<g<<endl;}
    if(h!=0)
    {cout<<"h= "<<h<<endl;}
    if(i!=0)
    {cout<<"i= "<<i<<endl;}
    if(j!=0)
    {cout<<"j= "<<j<<endl;}
    if(k!=0)
    {cout<<"k= "<<k<<endl;}
    if(l!=0)
    {cout<<"l= "<<l<<endl;}
    if(m!=0)
    {cout<<"m= "<<m<<endl;}
    if(n!=0)
    {cout<<"n= "<<n<<endl;}
    if(o!=0)
    {cout<<"o= "<<o<<endl;}
    if(p!=0)
    {cout<<"p= "<<p<<endl;}
    if(q!=0)
    {cout<<"q= "<<q<<endl;}
    if(r!=0)
    {cout<<"r= "<<q<<endl;}
    if(s!=0)
    {cout<<"s= "<<s<<endl;}
    if(t!=0)
    {cout<<"t= "<<t<<endl;}
    if(u!=0)
    {cout<<"u= "<<u<<endl;}
    if(v!=0)
    {cout<<"v= "<<v<<endl;}
    if(w!=0)
    {cout<<"w= "<<w<<endl;}
    if(x!=0)
    {cout<<"x= "<<x<<endl;}
    if(y!=0)
    {cout<<"y= "<<y<<endl;}
    if(z!=0)
    {cout<<"z= "<<z<<endl;}
}
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
44
45
#include <map>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	const int max_len(999);
	char che[max_len];

	cout<<"Enter the string you wish to process.\n";
	cin.getline(che, max_len);

	map<char, int> freqs;

	int i = 0;
	while (che[i] != '\0')
	{
		if (isalpha(che[i]))
		{
			che[i] = tolower(che[i]);
			freqs[che[i]] = 0;
		}

		++i;
	}

	i = 0;
	while (che[i] != '\0')
	{
		if (isalpha(che[i]))
		{
			freqs[che[i]] += 1;
		}

		++i;
	}

	cout << setw(6) << "letter" << " " << setw(6) << "frequency" << endl;
	for (auto ltr : freqs)
	{
		cout << setw(6) << ltr.first << " " << setw(6) << ltr.second << endl;
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cctype>

void freq(const char* begin, const char* end)
{
    int ftable[128] = { 0 };
    for(const char* cp = begin; cp != end; ++cp)
        ++ftable[::tolower(*cp)];
    std::cout << "\nThe frequency of each alphabet in the given string is:\n";
    for(char c = 'a'; c != 'z' + 1; ++c)
        if(ftable[c] != 0)
            std::cout << c << "= " << ftable[c] << '\n';
}

int main()
{
    char  data[999];
    char* end = data;
    std::cout << "Enter the string you wish to process.\n";
    std::cin.getline(data, 999);
    while(*end != '\0')
        ++end;
    freq(data, end);
}


Last edited on
Oh yeah, forgot to #include<cctype> in my earlier post.

1
2
3
4
5
6
7
8
9
10
11
12
void freq( const char* cstr ) // *** const
{
    if(cstr)
    {
        std::map<char,int> freq ;

        // character  classification is based on the currently set C locale
        for( ; *cstr ; ++cstr ) if( std::isalpha(*cstr) ) ++freq[ std::tolower(*cstr) ] ;

        for( auto pair : freq ) std::cout << pair.first << ' ' << pair.second << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/c84984dfe8dfd1cd
What is a "string related function"? You can't use std::string? That's just... well... I won't say it.
Topic archived. No new replies allowed.