Как искать слово в строке во FreeBASIC - PullRequest
0 голосов
/ 04 марта 2020

Я пытаюсь создать внутреннюю функцию в моей программе FreeBASI C, где я хочу проверить слово "echo" в строковой переменной "line0", и если "echo" является частью строки, я хочу ее повторить ввод (кроме «эхо»)

Ответы [ 2 ]

0 голосов
/ 05 апреля 2020

Это простой код, в котором вы можете использовать UCASE (String) для идентификации эха o Echo, не обращая внимания на регистр, а затем слово с:

mid (строка, начало как целое число, длина как целое число )

пример print mid ("Hello word!", 1, 5) (дать это сообщение: Hello)

' whe Declare a function where whe put all istruction
declare function view_echo(ByVal MyString as String)as Integer

' answer is a null variable we need only for take a result from a function
dim answer as integer

' make a function with name view_echo(string) 
function view_echo(ByVal MyString as String)as Integer

'In this case we print only if ECHO is the first word of the string'

'MyWord is a string variable where we take the result after read the orign

Dim MyWord as String
MyWord = "" ' initalize MyWord with no content inside

' this work only if the first word is echo doesnt care if upper case or lower
if UCASE(Mid(MyString, 1, 5)) = "ECHO " Then  

    ' len(MyString) give the lenght of MyString
    ' mid(MyString,1,5) give the word Echo 
    ' this follow row put inside MyWord variable the content of MyString
    ' starting from the 6th char wiht len we can have le leght of entere
    ' string
    MyWord = Mid(MyString, 6, len(MyString) - 5 ) 

end if
    Print MyWord 'Print the content of MyWords

return 0    
end function


'initializa the var line0 like string
dim line0 as String

' put inside line0 the string = Echo hello Word !
line0 ="Echo Hello Word !"

'Then answer ask the result to the function giving the line0 value string
'that let do the work where was make the function.
answer = view_echo(line0)
0 голосов
/ 06 марта 2020

ты проводил какие-либо исследования? Извините, но я полагаю, вы этого не сделали.

Ответ: уже есть функция для этой задачи, встроенная в язык Basi c.

Функция, которую вы ищете - "INSTR". Пожалуйста, ознакомьтесь с доступной документацией для FreeBasi c. Если вы затем решите попробовать написать свою собственную функцию INSTR (если вам нужна функция, которая не предоставляется встроенной функцией), попробуйте выполнить кодирование, и если вы застряли, мы постараемся помочь.

Поэтому ваша описанная задача будет включать следующие функции:

INSTR  ' check if the string is here
LEN    ' to know the length of your search string
MID    ' to create the 'reduced' output (maybe you will to have it used twice)
...