How to fix Errors and change my code. Tips

#include <stdio.h>
#include <conio.h>
int main()
{
char str[80];
int cnt[122];
int i;
for (i=0;i<122;++i)
cnt[i]=0;
clrscr();
printf("Enter a string less than 80 characters: ");
gets( str );
int wrong = 0;
for(i=0; str[i] != '\0'; ++i)
{
if (((str[i]>='a') && (str[i]<='z'))||((str[i]>='A') && (str[i]<='Z')))
{
++cnt[str[i]];
}
else
{
printf("Wrong Input.\n");
return 1;
}
}
for (i=0;i<122;++i)
{
if (cnt[i]>0)
{
printf ("%c = %d\n",(char)i,cnt[i]);
}
}

getch();
return 0;
}

1. when i run the program it says "Expression syntax error on function main." in the expression int wrong = 0;

2. and when i enter a numerical character. it doesn't say Wrong input, although we put Wrong input in else, but instead it closes the program and turns back to the blue IDE. It should say wrong input and will require a user to enter another word.

3. and when i enter a compound word like mother-in-law , it will display the character '-' and will show how many times it appear.
Though it should not be, only the alphabet would be display e.g. (mother-in-law = a,e,h,i,l,m,n,r,t,w) the dash must not be displayed.
Last edited on
[code] "Please use code tags" [/code]
1_ If you've got a syntax error, it should not compile. ISO C90 forbids mixed declarations and code
2_ It probably prints it, but you don't see it.
1
2
printf("Wrong Input.\n");
return 1; //this terminates your program 

3_ If you don't want to display it, then don't display it.

Arrays go from 0 to n-1, so cnt['z'] is out of bounds. However you only 'z'-'a'+1 cells.
You shouldn't use gets(), it's dangerous.
Topic archived. No new replies allowed.