Write a program in which a text string of Latin letters and auxiliary characters is first entered upon request. It is required to replace in it all the occurrences of the small letter “a” by 1, the small letter “b” by 22, “c” - by 333. In the new line, count the number of digits. The final line and the number of digits to issue on the screen. It turned out just that ... but nothing goes

Dim Len, k,m,n, i As Integer Dim Str, StrNew As String Dim Ch As Char Str = InputBox("Введите строку из латинских букв и вспомогательных символов") For i = 1 To Len 'Проход по строке Str Ch = Mid(Str, i, 1) 'Выделение очередного символа If Ch = "a" Then k = k + 1 ' Сравнение символа с буквой а и подсчет вхождений If Ch = "b" Then n = m + 22 ' Сравнение символа с буквой b и подсчет вхождений If Ch = "c" Then n = n + 333 ' Сравнение символа с буквой c и подсчет вхождений Next i MsgBox(" Число вхождений малой латинской буквы а равно ") MsgBox(" Число вхождений малой латинской буквы b равно ") MsgBox(" Число вхождений малой латинской буквы c равно ") End 
  • Hm And where is the replacement in your code? See: Ch = Mid(Str, i, 1) received another character, If Ch = "a" checked if it was not "a" , but what does k = k + 1 go k = k + 1 and what do you really need? And the Len value is not calculated anywhere, where can it be correct? - VladD
  • Something bad for me from such a solution can be solved firstly by a simpler problem, counting only the letters "a" for example - sercxjo

1 answer 1

Len - the word is reserved, you should not declare it, there will be uncertainties.

 Dim S as String, S_new as String, S_clear as String Dim counterA as Integer S = InputBox("Введите строку из латинских букв и вспомогательных символов") ' Заменяем S_new = Replace(S, "a", "1") S_new = Replace(S_new, "b", "22") S_new = Replace(S_new, "c", "333") ' Убираем цифры из строки S_clear = Replace(S_new, "1", "") S_clear = Replace(S_clear, "2", "") S_clear = Replace(S_clear, "3", "") S_clear = Replace(S_clear, "4", "") S_clear = Replace(S_clear, "5", "") S_clear = Replace(S_clear, "6", "") S_clear = Replace(S_clear, "7", "") S_clear = Replace(S_clear, "8", "") S_clear = Replace(S_clear, "9", "") S_clear = Replace(S_clear, "0", "") ' Считаем, на сколько уменьшилась длина строки при удалении цифр counterA = Len(S_new) - Len(S_clear) MsgBox("Итоговая строка: " & S_new & "," & vbCrLf & "Число цифр: " & CStr(counterA)) 

Let me explain: vbCrLf is a line break. Connect different strings with an ampersand ( & )