plz help me out

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
kuul
Newbie
Posts: 5
Joined: Thu Jun 26, 2008 12:03 pm
Location: ghana
Contact:

plz help me out

Post by kuul »

write a program that takes a multi-word string from the keyboard amd counts how many times each letter shos up.
the count should be case insensitve. meaning "A" is the same as "a"
sample input
enter a string: i love to Sing and Dance

sample out put:
a=2
c=1
d=2
e=2
g=1
i=2
l=1
n=3
o=2
s=1
t=1
v=1
kuull
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Sounds like homework to me!

We don't do homework, but here are some hints.

1) Place the string into a variable$ and LCASE$ or UCASE$ it.

2) Use a loop to get each letter of the string using MID$.

3) USE the ASCII codes to determine the count. Look up the codes and the ASC function. You will need IF or CASE statements for the codes returned. You could also place the counts into an Array if you know how.

You probably have been taught this stuff already. You would be cheating if I said more!

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
linster
Newbie
Posts: 2
Joined: Tue May 30, 2006 8:31 am

Post by linster »

Actually with C it's very easy (I've been learning alot of languages while not around). Basically you do something similar to this:

#include <stdio>;
main (str[128]) {

int position;
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, x, y, z;
position = 1;
while (position <= 128) {
if (str[position] != ("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")) {
position++;
}
if (str[position] == "a" ) { a++; }
if (str[position] == "b" ) { b++; }
if (str[position] == "c" ) { c++; }
if (str[position] == "d" ) { d++; }
if (str[position] == "e" ) { e++; }
if (str[position] == "f" ) { f++; }
if (str[position] == "g" ) { g++; }
if (str[position] == "h" ) { h++; }
if (str[position] == "i" ) { i++; }
if (str[position] == "j" ) { j++; }
if (str[position] == "k" ) { k++; }
if (str[position] == "l" ) { l++; }
if (str[position] == "m" ) { m++; }
if (str[position] == "n" ) { n++; }
if (str[position] == "o" ) { o++; }
if (str[position] == "p" ) { p++; }
if (str[position] == "q" ) { q++; }
if (str[position] == "r" ) { r++; }
if (str[position] == "s" ) { s++; }
if (str[position] == "t" ) { t++; }
if (str[position] == "u" ) { u++; }
if (str[position] == "v" ) { v++; }
if (str[position] == "w" ) { w++; }
if (str[position] == "x" ) { x++; }
if (str[position] == "y" ) { y++; }
if (str[position] == "z" ) { z++; }
}

}




Ok, now other members of the board are going to yell at me for this not being in basic, or for solving someone's homework, but I actually provided a subtle hint, and a probable (but very bloated) solution.

TO Kuul:

What function in basic allows you to access part of a string just like it was in a character array?

What is wrong with my solution, did I forget to validate some input (does it have to do anything with case?)

How might I go about fixing that? (Think $CHR)

Would it be dangerous if someone entered a sentence with more than 128 characters? Why?

Please reply! :)
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post by TmEE »

C code makes less sense to me than someone else's asm code.

anyway, I wrote something that will do what's required and its 9 lines of QB code.

I had 2x FOR loops, I used UCASE$, MID$, CHR$, ASC and LEN. And all the results are printed on screen.
Mida sa loed ? Nagunii aru ei saa :P
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 »

kuul


people will be glad to help if you actually show the code you are working on and having trouble with and not just asking us if we will do your homework for you. its not the way we do things around here.

moderator
markm
Coder
Posts: 14
Joined: Thu May 01, 2008 11:08 am

Linster: Accessing a string like a character array

Post by markm »

>>>What function in basic allows you to access part of a string just like it was in a character array?

That's MID$(string, position, number of characters)

MID$(A$,I,1) is equivalent to A(I,1)
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post by TmEE »

Now my code is not wasted...

Code: Select all

a$ = "one two three testing testing"
a$ = ucase$(a$)
for j% = 0 to 255
c% = 0
for i% = 1 to len(a$)
if j% = asc(mid$(a$, i%, 1)) then c% = c% + 1
next i%
print chr$(j%) + "="; c%,
nexi j%
Mida sa loed ? Nagunii aru ei saa :P
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Kuul, where are you? We expect you to post again, either thanking those who helped you or asking help on some part that you don;t undrstand or can't get to work. Please respond.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

That's what you get, when somebody just writes all of the code for you.

Especially if it's wrong! Why would a CHEATER say thanks anyway? Also if the code don't work, they just cheat somewhere else..........

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

burger2227:

It's sad to feel that some, or most, of the new and young posters here. The way I try tomake sure I'm addressing an honestly interested young person is to ask, first, what their goal might be. If I get that answered, I offer to help review thiere code, etc. That is, I try to base my help on what they do, on their iniciative. I can keep that up, as long as I keep gitting positive indications that they are interested. As soon as they stop responding, so do I...
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Post Reply