Check my Program. Give me Suggestions please.
The machine problem is:
Write a program that will identify word that the user inputs and determine how many times that word uses the certain letters, that the word contains. And the letters will be in alphabetical order.
The program must not accept a numeric value and special characters except the "-" that is needed in compound words as input .
Example :
I enter the word "letters"
The program must show:
e = twice
l = once
r = once
s = once
t = twice
If I enter the word "add5@6" with number or special character in the input word
the program will say "Wrong input" and will require the user to input another word.
---------- Post added at 08:06 AM ---------- Previous post was at 08:03 AM ----------
#include <stdio.h>
#include <conio.h>
void main()
{
1. char str[80];
2. int i,a,b;
3. clrscr();
4. printf("Enter a string less than 80 characters: ");
5. gets( str );
6. i = 0;
7. a = 0;
8. b = 1;
9. while (str[i])
{
10. if (((str[i] >= 65) && (str[i] <= 90)) || ((str[i] >= 97) && (str[i] <= 122)))
{
11. if (str[a]==str[b]?printf("=%d", b): printf("=0"));
{
12. printf("%c\n",str[i]);
13. i++;
14. a++;
15. b++;
}
16. else
{
17. printf("Wrong Input.");
18. break();
}
}
}
19. getch();
}
---------- Post added at 08:15 AM ---------- Previous post was at 08:06 AM ----------
i have problem in :
line 1 that should be any number of input not only 80 but if i put str[] the program will say that i must put a number in it.
line 17&18 after the wrong input will appear the program must start again and ask the user to enter another in put in a new screen. i put break to break the wrong input loop.
I believe my program needs more adjustment in order to achieve the machine problem. I need your tips and hints from the experts.
Feel free to comment and add some suggestions.
Thanks.
8 replies to this topic
#1
Posted 17 January 2012 - 04:15 PM
|
|
|
#2
Posted 18 January 2012 - 07:55 AM
There's a good deal wrong with your program, which is going to require a lot of restructuring to get it to work. I'll just take it a few steps at a time. Firstly, though, please use CODE tags around your code. It makes it much easier to read as it preserves the indenting.
You're going to need to dynamically allocate this array then. You use malloc() and free() for that task.
That's because of this line:
You added a semicolon after it, which breaks the branching, since this line in and of itself is a complete statement.
Additionally, this comparison is totally wrong. You aren't comparing letters in the string with each other, you're just keeping a running count of how many times each character appears. I think an integer array should do the trick for that.
Lastly, and this is just me being a stickler, but I suggest you use fgets() on stdin instead of gets(), which doesn't allow you to specify a maximum string length. You're opening yourself up for buffer overflows with gets().
renren00 said:
line 1 that should be any number of input not only 80 but if i put str[] the program will say that i must put a number in it.
You're going to need to dynamically allocate this array then. You use malloc() and free() for that task.
renren00 said:
line 17&18 after the wrong input will appear the program must start again and ask the user to enter another in put in a new screen. i put break to break the wrong input loop.
That's because of this line:
if (str[a]==str[b]?printf("=%d", b): printf("=0"));
You added a semicolon after it, which breaks the branching, since this line in and of itself is a complete statement.
Additionally, this comparison is totally wrong. You aren't comparing letters in the string with each other, you're just keeping a running count of how many times each character appears. I think an integer array should do the trick for that.
Lastly, and this is just me being a stickler, but I suggest you use fgets() on stdin instead of gets(), which doesn't allow you to specify a maximum string length. You're opening yourself up for buffer overflows with gets().
Edited by gregwarner, 18 January 2012 - 08:25 AM.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 21 January 2012 - 10:18 PM
Adjust your line #10. Increase the range so as to include characters like '@', etc.
#4
Posted 24 January 2012 - 06:24 AM
#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;
}
If you need for text mode of answers else create array:
ans[1]="once"; ans[2]="twice"; //ets
and use it
for (i=0;i<122;++i)
{
if (cnt[i]>0)
{
printf ("%c = %d\n",(char)i,ans[cnt[i]]);
}
}
#5
Posted 25 January 2012 - 10:22 PM
@voral
the character array index is form 0-122 right?
but i need only 65-90 (for capital letters ),97-122 (for small letter) and the character '-' (dash for compound words).
By the Way thank you all for help me out.
@Voral thanks this would be a great help.
i wonder if this statement is correct to specify the characters i needed instead of starting from 0 up to 122.
if ((((str[i] >= 65) || (str[i] >= '97')) && ((str[i] <= 90) || (str[i] <= 122))) || (str[i] == 45))
or
if ((((str[i] >= 'A') || (str[i] >= 'a')) && ((str[i] <= 'Z') || (str[i] <= 'z'))) || (str[i] == '-'))
the character array index is form 0-122 right?
but i need only 65-90 (for capital letters ),97-122 (for small letter) and the character '-' (dash for compound words).
By the Way thank you all for help me out.
@Voral thanks this would be a great help.
i wonder if this statement is correct to specify the characters i needed instead of starting from 0 up to 122.
if ((((str[i] >= 65) || (str[i] >= '97')) && ((str[i] <= 90) || (str[i] <= 122))) || (str[i] == 45))
or
if ((((str[i] >= 'A') || (str[i] >= 'a')) && ((str[i] <= 'Z') || (str[i] <= 'z'))) || (str[i] == '-'))
#6
Posted 26 January 2012 - 08:33 PM
I answer here
You must program the same way as you think in human language
step 1
Step 2
"but i need only 65-90 (for capital letters ) OR 97-122 (for small letter) OR the character '-' (dash for compound words)."
this means
str[i]>=65 OR str[i] >= 97 - equivalent to: str[i]>=65
(str[i] <= 90) OR (str[i] <= 122) - equivalent to: str[i]<=122
So you said:
"I need only 65 - 122 or character '-'"
:)
You must program the same way as you think in human language
step 1
renren00 said:
but i need only 65-90 (for capital letters ),
((str[i]>='A') && (str[i]<='Z')) or ((str[i]>=65) && (str[i]<=90))
renren00 said:
97-122 (for small letter)
((str[i]>='a') && (str[i]<='z')) or ((str[i]>=97) && (str[i]<=122))
renren00 said:
and the character '-' (dash for compound words).
(str[i]=='-') or (str[i]==45)
Step 2
"but i need only 65-90 (for capital letters ) OR 97-122 (for small letter) OR the character '-' (dash for compound words)."
( ((str[i]>='A') && (str[i]<='Z')) || ((str[i]>='a') && (str[i]<='z')) || (str[i]=='-') ) inline: (((str[i]>='A') && (str[i]<='Z'))||((str[i]>='a') && (str[i]<='z'))||(str[i]=='-')) or (((str[i]>=65) && (str[i]<=90))||((str[i]>=97) && (str[i]<=122))||(str[i]==45))
renren00 said:
((((str[i] >= 65) || (str[i] >= '97')) && ((str[i] <= 90) || (str[i] <= 122))) || (str[i] == 45))
renren00 said:
(
(
((str[i] >= 65) || (str[i] >= 97))
&&
((str[i] <= 90) || (str[i] <= 122))
)
||
(str[i] == 45)
)
(
((str[i] >= 65) || (str[i] >= 97))
&&
((str[i] <= 90) || (str[i] <= 122))
)
||
(str[i] == 45)
)
(str[i] <= 90) OR (str[i] <= 122) - equivalent to: str[i]<=122
So you said:
"I need only 65 - 122 or character '-'"
:)
#7
Posted 26 January 2012 - 09:12 PM
thanks @voral i understand now. ill try do make the program and i might ask some questions again
#8
Posted 27 January 2012 - 10:58 PM
{
if (((str[i]>='a') && (str[i]<='z'))||((str[i]>='A') && (str[i]<='Z')))
{
++cnt[str[i]];
}
else
{
printf("Wrong Input.\n");
return 1;
}
What does return 1 do in this statement block?
if (((str[i]>='a') && (str[i]<='z'))||((str[i]>='A') && (str[i]<='Z')))
{
++cnt[str[i]];
}
else
{
printf("Wrong Input.\n");
return 1;
}
What does return 1 do in this statement block?
#9
Posted 28 January 2012 - 06:25 PM
That is not a complete program, so answering the question is almost impossible.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









