Newbie count problem

P

Pete

Below I have attempted to write code to check through the alphabet 1 letter
at a time checking the frequency of each letter in the hard coded string and
printing it to screen. The code below goes into an endless loop.

Can somone please suggest the correct method for acheiving the correct
outcome.


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



unsigned int i,j;
int count=0;

main()


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

char* str = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\0n";

while(str!= "\0") {
for(i=0; i<27; i++)
for(j=0; j<120; j++)
{
if
(Alphab==str[j]);
count++;
printf("The letter %c has been used %d times ", Alphab, count);

}
}
}
 
P

pete

Pete said:
Below I have attempted to write code to check through the alphabet 1 letter
at a time checking the frequency of each letter in the hard coded string and
printing it to screen. The code below goes into an endless loop.

Can somone please suggest the correct method for acheiving the correct
outcome.

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

unsigned int i,j;
int count=0;

main()

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

char* str = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\0n";

while(str!= "\0") {
for(i=0; i<27; i++)
for(j=0; j<120; j++)
{
if
(Alphab==str[j]);
count++;
printf("The letter %c has been used %d times ", Alphab, count);

}
}
}


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

int main(void)
{
char Alphab[26] = "abcdefghijklmnopqrstuvwxyz";
char* str = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\0n";
size_t letter, character;
int count;

for (letter = 0; letter != 26; ++letter) {
count = 0;
for (character = 0; str[character]; ++character) {
if (Alphab[letter] == str[character]) {
++count;
}
}
printf("The letter %c has been used %d times\n",
Alphab[letter], count);
}
return 0;
}
 
E

Eric Bernard

Pete said:
Below I have attempted to write code to check through the alphabet 1 letter
at a time checking the frequency of each letter in the hard coded string and
printing it to screen. The code below goes into an endless loop.

Can somone please suggest the correct method for acheiving the correct
outcome.


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



unsigned int i,j;
int count=0;

main()


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

char* str = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\0n";

while(str!= "\0") {
for(i=0; i<27; i++)
for(j=0; j<120; j++)
{
if
(Alphab==str[j]);
count++;
printf("The letter %c has been used %d times ", Alphab, count);

}
}
}


It causes an endless loop because you are not incrementing str. You should
do something like while (!str[0]) and str++ somewhere.
That's not exactly how I would do it. More something like this:

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

int main(void)
{
unsigned int alpha[26] = { 0 }; /* zero-initialization for all items
*/
unsigned int others = 0;
char tmp;
int i;

char *str = "whatever string";

while (*str)
{
tmp = tolower(*str);
if (tmp >= 'a' && tmp <= 'z') /* Look up an ASCII table for
details */
alpha[tmp - 'a']++;
else
others++;

str++;
}

for (i = 0; i < 26; i++)
{
if (alpha != 0)
printf("The letter %c has been used %d times.\n", 'a' + i,
alpha);
}
if (others != 0)
printf("There were %i other characters.\n", others);
}
 
J

Jeff

Eric Bernard said:
Pete said:
Below I have attempted to write code to check through the alphabet 1 letter
at a time checking the frequency of each letter in the hard coded string and
printing it to screen. The code below goes into an endless loop.

Can somone please suggest the correct method for acheiving the correct
outcome.
[snip]


It causes an endless loop because you are not incrementing str. You should
do something like while (!str[0]) and str++ somewhere.
That's not exactly how I would do it. More something like this:

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

int main(void)
{
unsigned int alpha[26] = { 0 }; /* zero-initialization for all items
*/
unsigned int others = 0;
char tmp;
int i;

char *str = "whatever string";

while (*str)
{
tmp = tolower(*str);
if (tmp >= 'a' && tmp <= 'z') /* Look up an ASCII table for
details */
alpha[tmp - 'a']++;

You cannot assume that the value of 'a' and 'z' is consecutive. From the standard, the values of the
members of the execution character set are implementation-defined.

[snip]
 
D

Darrell Grainger

Below I have attempted to write code to check through the alphabet 1 letter
at a time checking the frequency of each letter in the hard coded string and
printing it to screen. The code below goes into an endless loop.

Can somone please suggest the correct method for acheiving the correct
outcome.

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

unsigned int i,j;
int count=0;

Ugly. Maintaining code with global variables is a pain. Using global
variables when there is absolutely no need to is just wrong.

Common mistake #1: This does not define main as a function that returns
void. This defines main as a function that returns an int. Knowing that
you will make common mistake #2: main should be defined as returning an
int. Do not change this to 'void main(void)'. Instead, add a 'return 0;'
at the end of the program.
{
char Alphab[26]={'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

It is easier to type:

char Alphab[] "abcdefghijklmnopqrstuvwxyz";
char* str = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\0n";

I'm going to guess that the "\0n" at the end of the string is a typing
mistake. No real harm.
while(str!= "\0") {

Problem here. The variable str is a pointer. It contains the address of
the literal string created above. The string "\0" is a different literal
string located at a different memory location. This expression will also
be true. This is your infinite loop.

Looking at the body of the while loop, I get the impression that this
while loop just should not exist at all. Whatever you thought it was
doing, get rid of it but keep the body.
for(i=0; i<27; i++)
for(j=0; j<120; j++)
{
if
(Alphab==str[j]);
count++;
printf("The letter %c has been used %d times ", Alphab, count);

}


LOTS of trouble here. The i-loop is going through all the letters of the
alphabet I am guessing. I would use:

for(i=0; i < sizeof Alphab; i++)

This way, if you switch to a different alphabet the for loop will adjust
automatically. You are creating an important link between the maximum value
of i and the size of the Alphab array. This will prevent an index out of
bounds error.

The same holds true for the j-loop too. Unfortunately, the size of the str
will give you the size of the pointer and not the size of what it points
at. You can either change the variable str to be:

char str[] = "it was disclosed yesterday that several informal\n"
"but direct contacts have been made with political\n"
"representatives of the viet cong in moscow\n";

or use strlen(str) to find the length of the string.

Your use of indented could use improving as well. That might just be your
editor or how you pasted the source into your email.

Additionally, there are better ways to do this. If this is your first
attempt, not bad. Keep working at it though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top