-3

Successfully counted each letter but letter order can't display sequentially as in word. For example- Input word-bbinood Output=b2i1n1o2d1

Here my program in qbasic:

INPUT "Enter the string:", A$
n = LEN(A$)

FOR i = 97 TO 122

    FOR j = 1 TO n
        IF CHR$(i) = MID$(A$, j, 1) THEN
            count = count + 1
        END IF
    NEXT
    FOR j = 1 TO n
        IF (MID$(A$, j, 1) = CHR$(i)) THEN
            PRINT CHR$(i), count
            j = n
        END IF

    NEXT

    count = 0
NEXT
anonymous2
  • 219
  • 7
  • 21
Binod Thakur
  • 53
  • 1
  • 6
  • 1
    Can you reword your question? It's quite confusing to comprehend. – Manav Dubey May 01 '17 at 12:13
  • question is easy to understand- Input : bbinnod then output should be in format : b2i1n2o1 – Binod Thakur May 01 '17 at 12:28
  • 1
    @BinodThakur, it is not up to you to tell people that your question is easy to understand. If somebody asks you to explain your question better, just to it. – Jens Gustedt May 01 '17 at 12:36
  • sorry guys i confess i did mistake. Well what question is counting frequency of each letter of word and letter order should be in same word letter.For example if input word is-bbinnod then output should be b2i1n2o1d1. – Binod Thakur May 01 '17 at 13:33

3 Answers3

2

There are two ways to answer this.

The first is a simple run-length encoding scheme, where the input cabc results in c1 a1 b1 c1. This has the benefit that you can often output things immediately with a fairly low memory requirement to boot:

input-string := Get-Input

(* nil is the representation of no characters here. *)
last-char := nil
count := 0

For Each (char c) In input-string
    If c = last-char Then
        count := count + 1
    Else
        If last-char = nil Then
            count := 1
            last-char := c
        Else
            Display last-char, count
            count := 1
            last-char := c
        End If
    End If
Loop
If count != 0 Then
    Display last-char, count
End If

The other solution I conceived will preserve the order and determine the counts of all unique letters in the string, yielding c2 a1 b1 for an input of cabc. This solution is a bit more complex and requires more memory and often more execution time, but it results in more compact output due to the lack of repeated letters:

input-string := Get-Input

(* 26 is the number of letters a-z. *)
counts := Create-Array 26
order-string := ""

For Each (char c) In input-string
    i := Locate-Char order-string, c
    If i = 0 Then
        order-string := order-string + c
        counts [Length order-string] := 1
    Else
        counts [i] := counts [i] + 1
    End If
Loop

For i := 1 To (Length order-string)
    Display (Char-At order-string, i), counts [i]
Loop

The first should be straightforward to convert to QBASIC, but you might need to use the help file to learn about the DIM keyword and how to use it to create an array. This algorithm assumes that arrays start at 1, not 0.

  • @BinodThakur If my answer helped you, feel free to upvote and/or accept it. Good luck in the future :) –  May 03 '17 at 21:29
2

Okay, here's code that should work in Qbasic.

DEFINT A-Z
DIM char(1 TO 255) AS STRING * 1
DIM outp(1 TO 255) AS STRING
INPUT "Type your string: ", inp$

'**** Comment out the following line if you want upper and lower cases
'**** treated separately:
inp$ = LCASE$(inp$)

FOR i = 1 TO LEN(inp$)
  char(i) = MID$(inp$, i, 1)
NEXT i

l = 0
FOR i = 1 TO LEN(inp$)
  k = 1
  FOR j = 1 TO i - 1
    IF char(j) = char(i) THEN GOTO skplet
  NEXT j
  l = l + 1
  FOR j = i + 1 TO LEN(inp$)
    IF char(j) = char(i) THEN k = k + 1
  NEXT j
  outp(l) = char(i) + LTRIM$(STR$(k))
skplet:
NEXT i

FOR i = 1 TO l
  PRINT outp(i);
NEXT i

Note that, as remarked in the comment, upper and lower case will be treated as the same letters as this answer stands. If you want them to be treated separately, simply delete or comment out the inp$ = LCASE$(inp$) line. Hope this helps!

anonymous2
  • 219
  • 7
  • 21
1

suggest 2 arrays, each 26 (if only considering alphabetic letters) that array

size_t counts[26] = {0};

then a second array `

char order[26] = {'\0'};

then for each letter in the input string.

if( isalpha( str[i]) ) 
{ 
    letter = tolower( str[i];
    counts[ letter - 'a' ]++; 

then loop through order if match found the do nothing, else replace '\0' with letter

printing out would be loop through order[] until either '\0' encountered or all elements examined.

At each order[] != '\0':

putc( order[ element ]);
printf( "%d", counts[ element-'\a' ] );
user3629249
  • 16,402
  • 1
  • 16
  • 17