0

我正在尝试用C 中的(三个下划线)替换' '(空格) 。'___'

这是我的代码:

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

int main()
{
   char *a = "12 34 56";
   int a_l = strlen(a);
   printf("str1: \"%s\" (%d)\n", a, a_l);

   char *b = "___";
   int b_l = strlen(b);
   printf("str2: \"%s\" (%d)\n", b, b_l);

   for (int i = 0; i < a_l; i++) {
      if (a[i] == ' ') {
         char *o = malloc(a_l + b_l);

         strncpy(o, a, i);
         strncpy(o + i, b, a_l);
         //strncpy help

         printf("out:  \"%s\"\n", o);
      }
   }

   return 0;
}

我认为到目前为止它是正确的,但我需要用正确的替换注释行strncpy(取字符串的其余部分a(不包括空格)并将其附加到 string o)。所以输出应该是这样的:

str1: "12 34 56" (8)
str2: "___" (3)
out:  "12___34 56"
out:  "12 34___56"

如果我的代码中还有其他错误,请告诉我。

UPD:这不应该替换循环中的所有空格。如果源字符串包含 8 个空格,则应该打印 8 行,并且每行只替换一个空格。

4

6 回答 6

6

你太复杂了,我只是 TL;DR。

可能 肯定想阅读、学习、接受和使用的一些评论:

I.int不适用于字符串长度和东西。size_t用于字符串长度和东西。

二、字符串文字不能被修改,因此使用遗留char *类型将它们分配给变量无论如何都不好,-const限定了那个糟糕的指针基类型。

三、malloc()如果真的不需要动态内存管理(我们不再生活在 1989 年),请使用 VLA 。

四。NUL- 终止您的字符串,因为 C stdlib 例程希望您这样做。

int main()
{
    const char *in = "foo bar baz";
    int nspc = 0;

    for (const char *p = strchr(in, ' '); p; p = strchr(p + 1, ' '))
        nspc++;

    char buf[strlen(in) + nspc * 2 + 1];
    memset(buf, 0, sizeof(buf));

    const char *s = in;
    for (const char *p = strchr(s, ' '); p; p = strchr(s, ' ')) {
        strncat(buf, s, p - s);
        strcat(buf, "___");
        s = p + 1;
    }

    const char *end = in + strlen(in);
    strncat(buf, s, end - s);

    printf("%s\n", buf);
    return 0;
}
于 2013-06-30T21:05:02.203 回答
2

你可以试试这个。问题在于你的 malloc 中的 a_l + b_l 总是相同的值。它不受空格数的影响。

int count = 0, index = 0;
for (int i = 0;  i < a_l;  ++i) {
    if (a[i] == ' ') {
        count++;
    }
}

const char *o = malloc(a_l + 2 * count + 1); // 2 is because you add 3 new symbols, but remove 1 so 3 - 1 = 2
memset(o, 0, sizeof(o));

for (int i = 0;  i < a_l;  ++i) {
    if (a[i] != ' ')
        o[index++] = a[i];
    else {
        o[index++] = '_';
        o[index++] = '_';
        o[index++] = '_';
    }
}
于 2013-06-30T20:59:43.793 回答
2

不使用库函数!!!

while(*string)
        {
                  if(*string=='\\')
                {
                        *string++;
                        while(repl_len--)
                         *dest++ = *repl_string++;
                }               
                else
                {
                        *dest++ = *string++;
                }
           repl_string = temp_repl_string;
           repl_len = temp_repl_len;    
        }
于 2015-06-05T11:33:50.793 回答
0
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

size_t my_strlen(const char *str, size_t *spc){
    size_t len;
    *spc = len = 0;
    while(*str){
        ++len;
        if(*str++ == ' ')++*spc;
    }
    return len;
}

int main(void){
   char *a = "12 34 56";
   size_t spc;
   int a_l = my_strlen(a, &spc);
   printf("str1: \"%s\" (%d)\n", a, a_l);

   char *b = "___";
   int b_l = strlen(b);
   printf("str2: \"%s\" (%d)\n", b, b_l);

   char *p, *o = malloc(a_l - spc + spc * b_l + 1);
   p=o;
   for (int i = 0; i < a_l; i++) {
      if(a[i] == ' ') {
         strncpy(p, b, b_l);
         p += b_l;
      } else {
         *p++ = a[i];
      }
   }
   *p = '\0';
   printf("out:  \"%s\"\n", o);
   free(o);
   return 0;
}
于 2013-06-30T21:09:01.120 回答
0

我看到已经添加了许多答案,但这可能是通过一个循环非常简单地完成的。由于字符串很短,您可以牺牲 CPU 内存并分配一个比原始字符串大 3 倍 +1 的数组,并确保它不会溢出:

const char* string= "12 34 56";
size_t length= strlen(string);
char result[length*3 +1];
unsigned int index= 0;
memset(result, '\0', length*3+1);
for(int i=0; i<length; i++)
{
    if(string[i]!= ' ')
    {
        result[index++]= string[i];
    }
    else
    {
        strcat(result,"___");
        index+= 3;
    }
}
于 2013-06-30T23:15:26.043 回答
0

我找到了另一个线程,在阅读了答案后,我发现正确的行应该是这样的:

strncpy(o + i + b_l, a + i + 1, a_l - i);

在此线程中建议进行一些修复后,我的代码如下所示:

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

int main()
{
   char *a = "12 34 56";
   size_t a_l = strlen(a);
   printf("str1: \"%s\" (%d)\n", a, a_l);

   char *b = "___";
   size_t b_l = strlen(b);
   printf("str2: \"%s\" (%d)\n", b, b_l);

   for (int i = 0; i < a_l; i++) {
      if (a[i] == ' ') {
         char *o = malloc(a_l + b_l);

         strncpy(o, a, i);
         strcpy(o + i, b);
         strncpy(o + i + b_l, a + i + 1, a_l - i);

         printf("out:  \"%s\"\n", o);
             free(o);
      }
   }

   return 0;
}

这将打印所需的 putput。

于 2013-07-01T09:27:42.967 回答