Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 删除字符并将其移动到字符串中_C_String_Recursion - Fatal编程技术网

C 删除字符并将其移动到字符串中

C 删除字符并将其移动到字符串中,c,string,recursion,C,String,Recursion,我需要一个递归代码的想法,该代码删除字符串中的一个特定字符,并将所有其他sting字符一起移动 例如: “天气多云” 输入的字符是“e”: 结果: “天气多云” 我真的不知道如何开始,谢谢你的帮助。这可以通过多种方式完成。我现在想的是存储不允许的字符数组,它将过滤哪些字符应该显示或不显示。像下面这样的 #include <stdio.h> #include <string.h> // Global Scope variable declaration int notAl

我需要一个递归代码的想法,该代码删除字符串中的一个特定字符,并将所有其他sting字符一起移动

例如:

“天气多云”

输入的字符是“e”:

结果:

“天气多云”


我真的不知道如何开始,谢谢你的帮助。

这可以通过多种方式完成。我现在想的是存储
不允许的字符
数组,它将过滤哪些字符应该显示或不显示。像下面这样的

#include <stdio.h>
#include <string.h>

// Global Scope variable declaration
int notAllowedChar[128] = {0}; // 0 for allowed , 1 for not allowed
char inputString[100];


void recursion(int pos, int len) {

    if( pos >= len ) {
       printf("\n"); // new line
       return;
    }

    if( notAllowedChar[inputString[pos]]) {// not printing
       recursion( pos + 1 , len );
    }
    else {
       printf("%c", inputString[pos]);
       recursion( pos + 1 , len );
    }

}

int main() {

 gets(inputString);  // taking input String

    printf("Enter not allowed chars:: "); // here we can even run a loop for all of them
    char notAllowed;
    scanf("%c", &notAllowed);
    notAllowedChar[notAllowed] = 1;
    int len = strlen(inputString);
    recursion( 0 , len );

}
#包括
#包括
//全局范围变量声明
int notAllowedChar[128]={0};//0表示允许,1表示不允许
字符输入字符串[100];
无效递归(int pos,int len){
如果(位置>=len){
printf(“\n”);//新行
回来
}
如果(notAllowedChar[inputString[pos]]){//不打印
递归(pos+1,len);
}
否则{
printf(“%c”,inputString[pos]);
递归(pos+1,len);
}
}
int main(){
获取(inputString);//获取输入字符串
printf(“输入不允许的字符::”;//这里我们甚至可以为所有字符运行一个循环
不允许使用字符;
scanf(“%c”,不允许使用(&N);
notAllowedChar[notAllowed]=1;
int len=strlen(输入字符串);
递归(0,len);
}
这是怎么回事 假设我们有一个简单的字符串“Hello world” 我们希望l应该从最终字符串中删除,所以最终输出将是“Heo word”

这里的“Hello world”长度为11个字符 在调用递归函数之前,我们要确保在
数组中的'l'索引是108个ascii值1

现在我们用(0,11)值调用递归方法,在递归方法中,我们主要有两个逻辑if操作,第一个是用于
基本情况的
,当pos等于或大于11时,我们将终止递归调用。如果它不是真的,我们将执行第二个逻辑操作,如果
current char
是否可打印。这只是检查此字符在
notAllowedChar
列表中的位置。每次我们增加
pos
value+
1
并执行递归调用,最后当pos等于或大于11时,这意味着我们已经决定是否打印字符,递归将终止。我尝试用有意义的名称分配变量。如果您仍然不明白这是如何工作的,您应该使用simple recursion simulation basic(在youtube上搜索),并且您应该尝试手动调试
递归局部范围中的值是如何变化的。这可能需要时间,但值得理解。祝你一切顺利

#包括
#include <stdio.h>

void RemoveChar(char* str, char chr) {
char *str_old = str;
char *str_new = str;

while (*str_old)
{
    *str_new = *str_old++;
    str_new += (*str_new != chr);
}

*str_new = '\0'; }

int main() {
char string[] = "the weather is cloudy";

RemoveChar(string, 'e');

printf("'%s'\n", string);

return 0; }
void RemoveChar(char*str,char-chr){ char*str_old=str; char*str_new=str; while(*str_old) { *str_new=*str_old++; str_new+=(*str_new!=chr); } *str_new='\0';} int main(){ char string[]=“天气多云”; RemoveChar(字符串“e”); printf(“%s”\n”,字符串); 返回0;}
一种简单的方法是在字符串上循环并添加与不需要的字母不匹配的任何字母

下面是一个演示:

char *source = "the weather is cloudy";
int source_len = strlen(source);

char *target = (char *)calloc(source_len, sizeof(char));
int target_len = 0;

char to_remove = 'e';

for(int i = 0; i < source_len; i++)
{
    if(source[i] != to_remove)
    {
        target[target_len++] = source[i];
    }
}

puts(target); // Output "th wathr is cloudy" in the console
char*source=“天气多云”;
int source_len=strlen(source);
char*target=(char*)calloc(source_len,sizeof(char));
int target_len=0;
char to_remove='e';
对于(int i=0;i
#包括
void remove_impl(字符*s、字符c、字符*d){
如果(*s!=c){
*d++=*s;
}
如果(*s!='\0'){
移除_impl(++s,c,d);
}
}
无效删除(字符*s,字符c){
移除_impl(s、c、s);
}
int main(){
char s[]=“天气多云”;
删除(s,'e');
卖出(s);
}
它是如何工作的?考虑<代码> ReaveIIMPL>代码>code>s
是原始字符串,
c
是要从
s
中删除的字符,
d
是结果字符串,其中写入的
s
字符不等于
c
。递归地遍历
s
的字符。如果下一个字符不等于
c
,则将其写入
d
。递归停止点是检查是否达到
s
结尾的条件。由于需要修改源字符串,因此实现了包装器(
remove
),其中作为d传递原始字符串(
s

#include <stdio.h>
#include <string.h>

char *remove_char(char *str, int c)
{
    char *pos;
    char *wrk = str;
    while((pos = strchr(wrk, c)))
    {
        strcpy(pos, pos + 1);
        wrk = pos;
    }
    return str;
}

int main()
{
    char str[] = "Hello World";
    printf(remove_char(str, 'l'));

    return 0;
}

两者都要求字符串是可写的(例如,您不能将指针传递到字符串文字)

轮到我提出建议了!我添加了一个断言测试并使用现有函数(strchr和strcpy)

#包括
#包括
#包括
int removeChar(char*str,char-chr)
{
断言(str!=0);//始终控制项!
char*str_pnt=strchr(str,chr);
如果(str_pnt){
strcpy(str_pnt,str_pnt+1);
removeChar(str_pnt,chr);
}
}
真空总管(真空)
{
char str[]=“天气多云”;
char char_to_delete='e';
removeChar(str,char\u to\u delete);
put(str);
}

#包括
/**
*返回删除的字符数。
*基本大小写:如果当前字符是空字符(字符串末尾)
*如果应该删除字符,则返回剩余字符串中删除的字符的1+个数。
*如果是其他字符,只需返回剩余字符串中删除的字符数
*/
int removeCAfterwardsAndCount(字符*s,字符c){
如果((*s)='\0'){
返回0;
}
如果((*s)==c){
int noOfChars=removeCAfterwardsAndCount(s+1,c);//s+1表示剩余字符串
s[noOfChars]=*s;//向前移动当前字符(*s)noOfChars位置
return noOfChars+1;//表示此字符已删除…应在此处复制其他字符。。。
}
否则{
int noOfChars=removeCAfter
#include <stdio.h>
#include <string.h>

char *remove_char(char *str, int c)
{
    char *pos;
    char *wrk = str;
    while((pos = strchr(wrk, c)))
    {
        strcpy(pos, pos + 1);
        wrk = pos;
    }
    return str;
}

int main()
{
    char str[] = "Hello World";
    printf(remove_char(str, 'l'));

    return 0;
}
char *remove_char(char *str, int c)
{
    char *pos = str;
    char *wrk = str;
    while(*wrk)
    {
        if(*wrk == c)
        {
            *wrk++;
            continue;
        }
        *pos++ = *wrk++;
    }
    *pos = 0;
    return str;
}
#include <string.h>
#include <stdio.h>
#include <assert.h>

int removeChar(char *str, char chr)
{
    assert(str != 0);                   // Always control entry !
    char *str_pnt = strchr(str, chr);   

    if (str_pnt) {
        strcpy(str_pnt, str_pnt+1); 
        removeChar(str_pnt, chr);
    }
}

void main (void)
{
    char str[] = "the weather is cloudy";
    char char_to_delete = 'e';

    removeChar(str, char_to_delete);
    puts(str);
}

#include <stdio.h>

/**
* Returns the number of removed chars.
* Base case: if the current char is the null char (end of the string)
* If the char should be deleted return 1 + no of chars removed in the remaining string.
* If it's a some other char simply return the number of chars removed in the remaining string
*/
int  removeCAfterwardsAndCount(char* s,char c){
  if((*s) == '\0'){
      return 0;
  }

  if((*s) == c){
     int noOfChars = removeCAfterwardsAndCount(s+1,c);// s+1 means the remaining string
      s[noOfChars] = *s; // move the current char (*s) noOfChars locations ahead

    return  noOfChars +1; // means this char is removed... some other char should be copied here...

  }
  else{
      int noOfChars  = removeCAfterwardsAndCount(s+1,c);
      s[noOfChars ] = *s;
    return  noOfChars ; // means this char is intact ... 
  }

}

int main()
{

    char s[] = "Arifullah Jan";
    printf("\n%s",s);
    int totalRemoved = removeCAfterwardsAndCount(s,'a');
    char *newS = &s[totalRemoved]; // the start of the string should now be originalPointer + total Number of chars removed

    printf("\n%s",newS);

    return 0;
}