12-29-2022, 01:28 AM
(This post was last modified: 12-29-2022, 01:34 AM by George McGinn.)
Awhile back I needed to remove SPACES (and sometimes other characters) from a string in some of my C code.
The need to do so in QB64 reared its ugly head again today, so I resurrected my code and now have incorporated it into any QB64 program I will need it in.
You pass the function 2 parameters - The string you want to change, and the character(s) you want removed. For example, if I want spaces and periods to be removed from a sentence, I pass " ." in the second argument. You don't need to run this function multiple times to accomplish this.
Here is the C/C++ Header file:
Here is the QB64 program that tests the above:
This is the output from running the above code:
The need to do so in QB64 reared its ugly head again today, so I resurrected my code and now have incorporated it into any QB64 program I will need it in.
You pass the function 2 parameters - The string you want to change, and the character(s) you want removed. For example, if I want spaces and periods to be removed from a sentence, I pass " ." in the second argument. You don't need to run this function multiple times to accomplish this.
Here is the C/C++ Header file:
Code: (Select All)
#include <stdio.h>
/* search for character(s) */
int string_search_chr(char *tokens,char s){
if (!tokens || s=='\0')
return 0;
for (;*tokens; tokens++)
if (*tokens == s)
return 1;
return 0;
}
char *string_remove_chr(char *str,const char *tokens) {
char *src = str , *dst = str;
/* validate input */
if (!(str && tokens))
return NULL;
while(*src)
if(string_search_chr(tokens,*src))
src++;
else
*dst++ = *src++; /* assign first, then incement */
*dst='\0';
return str;
}
Here is the QB64 program that tests the above:
Code: (Select All)
DECLARE LIBRARY "./removeStr"
FUNCTION __REMOVESPACES$ ALIAS string_remove_chr(qString$, charValue$)
END DECLARE
QBMain:
PRINT "QB64: Test #1 - Remove SPACES from 'New York Yankees"
qString$ = "New York Yankees"
retValue$ = __REMOVESPACES$(qString$+CHR$(0), " ")
PRINT "QB64: qString$ = "; qString$
PRINT "QB64: retValue$ = "; retValue$
PRINT
PRINT "QB64: Used in a PRINT stmt, function returns: "; __REMOVESPACES$(qString$+CHR$(0), " ")
PRINT
PRINT "QB64: Test #2 -Remove SPACES and PERIODS from 'New York Yankees."
qString$ = "New York Yankees"
retValue$ = __REMOVESPACES$(qString$+CHR$(0), " .")
PRINT "QB64: qString$ = "; qString$
PRINT "QB64: retValue$ = "; retValue$
PRINT
PRINT "QB64: Used in a PRINT stmt, function returns: "; __REMOVESPACES$(qString$+CHR$(0), " ")
PRINT
SYSTEM 0
This is the output from running the above code:
Quote:QB64: Test #1 - Remove SPACES from 'New York Yankees
QB64: qString$ = New York Yankees
QB64: retValue$ = NewYorkYankees
QB64: Used in a PRINT stmt, function returns: NewYorkYankees
QB64: Test #2 -Remove SPACES and PERIODS from 'New York Yankees.
QB64: qString$ = New York Yankees
QB64: retValue$ = NewYorkYankees
QB64: Used in a PRINT stmt, function returns: NewYorkYankees
------------------
(program exited with code: 0)
Press return to continue
—————————————————————————————
George McGinn
Theoretical/Applied Computer Science Specialist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
Society of American Baseball Research
George McGinn
Theoretical/Applied Computer Science Specialist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
Society of American Baseball Research