c - 字符串的第一个字符在哪里?

标签 c arrays string character

我正在尝试编写“C Primer Plus”书的练习。其中一次,我遇到了一些我无法解决或弄清楚发生了什么的事情。经过一步一步的调试试验,我刚刚测试了这个:

    #include <stdio.h>

    int main()
    {
        char str[500];
        gets(str);
        puts(str);

        return 0;
    }

输出(根据需要):

exact ly every thing I enter

exact ly every thing I enter

但是关于我正在尝试做的练习,它对超过 2 个连续空格很敏感。 gets() 后面紧跟着 puts();但我不知道出了什么问题。所以我引用整个代码:

/*
BUG: MORE THAN 1 SPACES NEXT TO EACHOTHER. WHERE THE FIRST CHARACTER GOES?!!

Write a function that takes a string as an argument and removes the spaces from the string.
Test it in a program that uses a loop to read lines until you enter an empty line.
The program should apply the function to each input string and display the result.
*/

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

int spaceRemover(char *in);
void takeBack(char *in);

int main(void)
{
    puts("Enter a string for the SPACE-REMOVER: (RETURN to quit)");
    do
    {
        char str[500];
        int spaces;
        gets(str);
        puts(str); //for debugging to know is it complete just after gets() ?
        //printf("\nFirst char of string: %c\n",str[0]);
        //printf("\nFirst Char: %p '%c'\n",str,*str);
        spaces=spaceRemover(str);
        printf("\n%d spaces removed: \n",spaces);
        puts(str);
        puts("\nEnter a string for the SPACE-REMOVER: (RETURN to quit)");
    }
    while (getchar() != '\n');

    return 0;
}

int spaceRemover(char *in)
{
    int count=0, i;
    for (i=0 ; i<strlen(in) ; i++)
        while ( *(in+i)==' ' )      //IF will skip more than one space; but WHILE won't
        {
            //printf("%p '%c' \t B4: %p '%c'\n",in+i,*(in+i),in+i-1,*(in+i-1));
            takeBack(in+i);
            count++;

        }
    return count;
}

void takeBack(char *spaceLocation)
{
    int j=0;
    while (*(spaceLocation+j)!= '\0' )
    {
        *(spaceLocation+j) = *(spaceLocation+j+1);
        //putchar(*(spaceLocation+j+1));
        j++;
    }

    return;
}

输出:

Enter a string for the SPACE-REMOVER: (RETURN to quit)

this is separated by single spaces

this is separated by single spaces


5 spaces removed: 

thisisseparatedbysinglespaces


Enter a string for the SPACE-REMOVER: (RETURN to quit)

I'll    try   more than   single space separators

'll    try   more than   single space separators

13 spaces removed: 

'lltrymorethansinglespaceseparators

注意:将 block 引用与此一起使用,会丢弃连续的空格。

这是怎么回事? 我的代码有什么问题导致这个吗?

(使用 Code::Blocks 和 gcc。)

最佳答案

Where the first character of the string goes?

您的第一个字符由 while (getchar() != '\n'); 中的 getchar 读取。当您输入字符串

I'll    try   more than   single space separators  

然后第一个字符Igetchar读取并与\n进行比较。因此,只留下

'll    try   more than   single space separators    

在输入缓冲区中由gets读取。将 main 主体更改为:

    char str[500];
    puts("Enter a string for the SPACE-REMOVER: (RETURN to quit)");
    gets(str);
    do
    {
        int spaces;
        puts(str); //for debugging to know is it complete just after gets() ?
        //printf("\nFirst char of string: %c\n",str[0]);
        //printf("\nFirst Char: %p '%c'\n",str,*str);
        spaces=spaceRemover(str);
        printf("\n%d spaces removed: \n",spaces);
        puts(str);
        puts("\nEnter a string for the SPACE-REMOVER: (RETURN to quit)");
        gets(str);
    }
    while (str[0]);

关于c - 字符串的第一个字符在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379805/

相关文章:

c - 用 C 优化时间?在一个简单的输入读取算法中

c - 如何指定目标文件的名称

c - 数组的 Scanf_s 错误

java - 按字母顺序排序数组列表时遇到问题

从 char16_t* 复制到 char16_t*

c - 为什么我的第二个 printf 没有按预期调用?

c - 我在 C 中实现了一个 Map 对象,但使用它会给我一个 Segmentation Fault

计数排序 - C

javascript - 转义字符串字符而无需手动转义它们

java - 如何剪切字符串中的关键字(Java)?