目前我能夠從字串中洗掉單個字符,但我無法弄清楚如何設定“符號”以便它可以洗掉多個字符......有什么建議嗎?
#include <string.h>
void remove_character(char *string, char *remove);
int main(void){
char s[] = "Thi!s is s!ome tex!t that we'!ll check for plagiarism";
char symbol[] = "!";
printf("%s\n", s);
remove_character(s, symbol);
printf("%s\n", s);
return(0);
}
void remove_character(char *string, char *remove){
int position = 0;
while (string[position] != '\0')
{
if (string[position] == *remove){
int newposition = position;
while (string[newposition] != '\0')
{
string[newposition] = string[newposition 1];
newposition ;
}
} else position ;
}
}
uj5u.com熱心網友回復:
您不必每次函式遇到要洗掉的字符時都“壓縮”字串。
#include <stdio.h>
#include <string.h>
// Define the function ahead of its use. It is its own prototype.
void remove_character( char *str, char *remove ) {
// copy characters, but only increment destination for "non-remove" characters.
for( size_t src = 0, dst = 0; ( str[dst] = str[src] ) != '\0'; src )
dst = (strchr( remove, str[dst] ) == NULL);
}
int main( void ) {
char s[] = "Thi!s is s!ome tex!t that we'!ll check for plagiarism";
char symbols[] = "!s"; // Stripping out '!' and 's'
puts( s ); // simpler
remove_character( s, symbols );
puts( s ); // simpler
return 0; // return is NOT a function call. No '()'.
}
Thi!s is s!ome tex!t that we'!ll check for plagiarism
Thi i ome text that we'll check for plagiarim
uj5u.com熱心網友回復:
如果你可以讓它洗掉 1 只需回圈一個可移動字符陣列的函式
void remove_character(char *string, char remove){
int position = 0;
while (string[position] != '\0')
{
if (string[position] == remove){
int newposition = position;
while (string[newposition] != '\0')
{
string[newposition] = string[newposition 1];
newposition ;
}
} else position ;
}
}
void remove_character(char *string, char *remove){
for(each char in remove){
remove_character(string, remove[index])
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520254.html
標籤:C细绳特点特殊字符
