Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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 - Fatal编程技术网

C编程如何替换字符串中的空格

C编程如何替换字符串中的空格,c,C,我一直试图让这段代码删除空格,并用“%”替换它们,但似乎无法让它正常工作。有人能告诉我我做错了什么吗 输入:狐狸跳过了月亮。 结果:狐狸跳过了月亮。 #include <stdio.h> #include <string.h> #include <stdlib.h> #define SPACE ' ' int main() { char string[100], *blank, *start; int length, c = 0, d = 0;

我一直试图让这段代码删除空格,并用“%”替换它们,但似乎无法让它正常工作。有人能告诉我我做错了什么吗

输入:
狐狸跳过了月亮。
结果:
狐狸跳过了月亮。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SPACE ' '

int main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;

   printf("Enter a string\n");
   gets(string);

   length = strlen(string);

   blank = string;

   start = (char*)malloc(length+1);

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
     d++;
      }
      c++;
   }
   *(start+d) = '\0';

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

   free(start);     

 system("PAUSE");
 }
预期结果: 输入:
狐狸跳过了月亮。
输出:
fox%跳过了%moon.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SPACE ' '

int main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;

   printf("Enter a string\n");
   gets(string);

   length = strlen(string);

   blank = string;

   start = (char*)malloc(length+1);

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
     d++;
      }
      c++;
   }
   *(start+d) = '\0';

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

   free(start);     

 system("PAUSE");
 }
#包括
#包括
#包括
#定义空间“”
int main()
{
字符字符串[100],*空白,*开始;
整数长度,c=0,d=0;
printf(“输入字符串”);
获取(字符串);
长度=strlen(字符串);
空白=字符串;
开始=(字符*)malloc(长度+1);
if(start==NULL)
退出(退出失败);
而(*(空白+c))
{
如果(*(空白+c)=空格和*(空白+c+1)=空格)
{}
其他的
{
*(开始+d)=*(空白+c);
d++;
}
C++;
}
*(开始+d)='\0';
printf(“%s\n”,开始);
自由(启动);
系统(“暂停”);
}

您的代码实际上是删除所有2个连续空格。 如果(*(空白+c)=空格和*(空白+c+1)=空格)替换为

 if ( *(blank+c) == SPACE)
  {
  *(start+d) = '%';
  d++;
  }
如果还希望折叠多个空格,则需要将前面的代码添加到If/else语句中

if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
  {}
else if ( *(blank+c) == SPACE)
  {
  *(start+d) = '%';
  d++;
  }
  else
  {
     *(start+d) = *(blank+c);
 d++;
  }
  c++;

您的代码似乎很难阅读。请尝试使用一些合理的变量名。下面是正在工作的while循环:-

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) != ' ' )
      {
          c++;
      }
      else
      {
          *(blank+c) = '%';
      }
   }
   *(blank+c+1) = '\0';

   printf("%s\n", blank);
#包括
#包括
#包括
#定义空间“”
int main(){
字符字符串[100],*开始,pv;
int c,d;
printf(“输入字符串”);
scanf(“%99[^\n]”,字符串);
start=malloc(strlen(string)+1);
if(start==NULL)
退出(退出失败);
pv=0;//上一个字符
对于(d=c=0;字符串[c];++c){
if(字符串[c]==空格){
如果(pv!=空间)
开始[d++]='%';
pv=空间;
}否则{
pv=开始[d++]=字符串[c];
}
}
开始[d]='\0';
printf(“%s\n”,开始);
自由(启动);
系统(“暂停”);
返回0;
}

第一个作业,选择语言。是C还是C++?这是调皮的C,因为你有一个Maloc铸件。这是C++中需要的,C中的坏风格需要编译所有警告和调试信息(<代码> Gcc-Walth-WOTU-G<代码>)。然后使用调试器(
gdb
),然后从
(char*)malloc
;-)中删除该
(char*)
。此外,这还不清楚您希望对连续空间执行什么操作。我编辑了你的问题,让它们出现在输入和输出页面中!你的编辑很好。是否有任何方法可以输出替换的空格数?是的,创建一个全局int变量,并在
if(*(blank+c)==SPACE)
部分递增它。我尝试使用if(blankblank是一个指针,因此我看不出将它与1进行比较有什么意义。您应该在开始时声明
int count=0;
,然后使用
count++;
inside
if(*(blank+c)=SPACE{…}
递增count,最后可以使用
printf(“替换%d空格,count”)输出)
谢谢,现在可以用了。我试过用if(blank@aab是否需要可以减少的空间数?我是指用“%”符号替换的空间数。如:“狐狸跳过了月亮。”将输出:%fox%跳过了%moon。替换的空间:5@aab分配“%”时计算数字。