Count character

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Count character

Post by Löwenherz »

Hello all

How I can Count the complete characters (String values numbers etcpp) of my freebasic example?
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Count character

Post by Löwenherz »

OK Made this example how many characters occurs in one String , all OK Here with result
(new edit)

Code: Select all

' Define the string
' fictive string below
Dim As String my_string = "screen 13:color 15,3:cls:line(100,100)-(150,150),,bf :dim as integer a = 10 : b,c,d : ""hello freebasic how are you?"": Rgb rnd 100 255 100 sleep locate 10,20"

' Initialize an array to store character counts (assuming ASCII characters)
Dim As Integer char_count(255)

' Iterate over the first 1024 characters of the string
For i As Integer = 1 To 1024
    ' Get the character
    Dim As String current_char = Mid(my_string, i, 1)
    
    ' Ignore spaces and commas
    If current_char <> " " And current_char <> "," Then
        ' Increment the count for the character
        char_count(Asc(current_char)) += 1
    End If
Next

' Print the character counts
For i As Integer = 0 To 255
    If char_count(i) > 0 Then
        Print "Character "; Chr(i); " occurs "; char_count(i); " times."
    End If
Next

sleep
fxm
Moderator
Posts: 12197
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count character

Post by fxm »

For i As Integer = 1 To 1024 Len(my_string)
(otherwise, lots of ghost null characters are counted from the the string end index up to 1024 index)
SARG
Posts: 1790
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Count character

Post by SARG »

Faster using string[n], no need an intermediate string. Warning zero based.

Code: Select all

Dim As String my_string = "screen 13:color 15,3:cls:line(100,100)-(150,150),,bf :dim as integer a = 10 : b,c,d : ""hello freebasic how are you?"": Rgb rnd 100 255 100 sleep locate 10,20"

' Initialize an array to store character counts (assuming ASCII characters)
Dim As Integer char_count(255)

' Iterate over the first 1024 characters of the string
For i As Integer = 0 To Len(my_string)-1
    ' Get the character
    ''''Dim As String current_char = Mid(my_string, i, 1)
    dim as integer vchar=my_string[i]
    ' Ignore spaces and commas
    '''''If current_char <> " " And current_char <> "," Then
    If vchar <> asc(" ") And vchar <> asc(",") Then
        ' Increment the count for the character
        char_count(vchar) += 1
    End If
Next

' Print the character counts
For i As Integer = 0 To 255
    If char_count(i) > 0 Then
        Print "Character "; Chr(i); " occurs "; char_count(i); " times."
    End If
Next
fxm
Moderator
Posts: 12197
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count character

Post by fxm »

For long strings (greater than 256 characters), where execution time becomes more noticeable, it is advantageous to filter certain characters only when displaying counters:

Code: Select all

Dim As String my_string = "screen 13:color 15,3:cls:line(100,100)-(150,150),,bf :dim as integer a = 10 : b,c,d : ""hello freebasic how are you?"": Rgb rnd 100 255 100 sleep locate 10,20"

' Initialize an array to store character counts (assuming ASCII characters)
Dim As Integer char_count(255)

' Iterate over the characters of the string
For i As Integer = 0 To Len(my_string)-1
    ' Increment the count for the character
    char_count(my_string[i]) += 1
Next

' Print the character counts
For i As Integer = 0 To 255
    ' Ignore spaces and commas
    If i <> asc(" ") And i <> asc(",") Then
        If char_count(i) > 0 Then
            Print "Character "; Chr(i); " occurs "; char_count(i); " times."
        End If
    End If
Next

Sleep
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Count character

Post by Löwenherz »

Thank you both Sarg and fxm

@fxm where I can Insert in your Code the complete result of all characters? Like in my First example Shows result at First line
fxm
Moderator
Posts: 12197
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count character

Post by fxm »

fxm wrote: Apr 22, 2024 11:55 For i As Integer = 1 To 1024 Len(my_string)
(otherwise, lots of ghost null characters are counted from the the string end index up to 1024 index)
Your first code displays in the first line the value corresponding to:
1024 - Len(my_string) = 1024 - 156 = 868

What exactly do you want to display on the first line?
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Count character

Post by Löwenherz »

No sorry misunderstanding I am meaning the result of all counts of characters AS a sum found in the String at the end of example :)

In my Fürst example the result after compiling showed at console Print at First Line
paul doe
Moderator
Posts: 1745
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Count character

Post by paul doe »

Löwenherz wrote: Apr 22, 2024 15:42 No sorry misunderstanding I am meaning the result of all counts of characters AS a sum found in the String at the end of example :)
The result of the sum of all counted characters equals the string length...
fxm
Moderator
Posts: 12197
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Count character

Post by fxm »

Code: Select all

Dim As String my_string = "screen 13:color 15,3:cls:line(100,100)-(150,150),,bf :dim as integer a = 10 : b,c,d : ""hello freebasic how are you?"": Rgb rnd 100 255 100 sleep locate 10,20"

' Initialize an array to store character counts (assuming ASCII characters)
Dim As Integer char_count(-1 TO 255)

' Iterate over the first 1024 characters of the string
For i As Integer = 0 To Len(my_string)-1
    ' Get the character
    ''''Dim As String current_char = Mid(my_string, i, 1)
    dim as integer vchar=my_string[i]
    ' Ignore spaces and commas
    '''''If current_char <> " " And current_char <> "," Then
    If vchar <> asc(" ") And vchar <> asc(",") Then
        ' Increment the count for the character
        char_count(vchar) += 1
        char_count(-1) += 1
    End If
Next

' Print the character counts
Print "Number of characters in the string"; Len(my_string)
Print
Print "Number of characters counted"; char_count(-1)
For i As Integer = 0 To 255
    If char_count(i) > 0 Then
        Print "   Character "; Chr(i); " occurs "; char_count(i); " times."
    End If
Next

Sleep
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Count character

Post by Löwenherz »

Thanks again for Feedback fxm :) I have now used your modified Version to adept for my example nice tuesday

Code: Select all

' - freebasic check length of a string, 21/23-04-2024, frank bruebach
' - working example string below, example runs, thx fxm :-)
'
Dim As String my_string = "Screen 13:color 15,3:cls:line(100,100)-(150,150),,bf:line(150,150)-(200,100),,bf:line(200,100)-(180,50),,bf:line(180,50)-(120,50),,bf:line(120,50)-(100,100),,bf:color 2,10:locate 10,20:? "";:color 2,11:locate 20,15:? ""Fr"";:color 2,12:locate 30,20:? ""e"";:color 2,13:locate 40,20:? ""e"";:color 2,14:locate 50,20:? ""B"";:color 2,15:locate 60,20:? ""a"";:color 2,0:locate 70,20:? ""s"";:color 2,1:locate 80,20:? ""iC"";:color 2,2:Sleep"

' Initialize an array to store character counts (assuming ASCII characters)
Dim As Integer char_count(-1 TO 255)

' Iterate over the first 1024 characters of the string
For i As Integer =  0 To Len(my_string)-1 '1 To 1024
    ' Get the character
    'Dim As String current_char = Mid(my_string, i, 1)
    Dim As integer current_char = my_string[i]
    ' Ignore spaces and commas
    '''''If current_char <> " " And current_char <> "," Then
    
    If current_char <> asc(" ") And current_char <> asc(",") Then
    ' Increment the count for the character
        char_count(current_char) += 1
        char_count(-1) += 1
    End If
Next

' Print the character counts
Print "Number of characters in the string"; Len(my_string)
Print
Print "Number of characters counted"; char_count(-1)
For i As Integer = 0 To 255
    If char_count(i) > 0 Then
        Print "   Character "; Chr(i); " occurs "; char_count(i); " times."
    End If
Next
Print "all ok with result" 
sleep
Post Reply