c - 交换 char 指针的值

标签 c string pointers char

我正在尝试一些东西......但我无法理解结果

#include<stdio.h>
#include<conio.h>

char *remove_vowels_string(char *p);

void main()
{
    clrscr();
    char str[77],*getString;
    gets(str);
    getString=remove_vowels_string(str);
    puts("\n");
    puts(getString);
    getch();
}

char *remove_vowels_string(char *p)
{
    char *q;
    q=p;
    while(*p)
    {
        if(*p=='a' || *p=='e' || *p=='i' || *p=='o' || *p=='u')
        {
            for(int i=0;*(p+i)!='\o';i++)
            // printf(" as %d",i);
            {
                 *q=*(p+i);
                 *(p+i)=*(p+i+1);
                if(*(p+i+1)=='\o')
                    break;

                    *(p+i+1)=*q;
            }

            printf("\n%c\n",*p);
            *(p+i+1)='\o';
        }
        p++;
    }
    puts(p);
    return p;
}

我想向前移动每个值,并用空指针替换元音字符。但该程序不起作用。请告诉我哪里出错了以及为什么出错。

最佳答案

\o\0 不同。 \o 将被视为 oReference

 for(int i=0;*(p+i)!='\o';i++)

应该是

 for(int i=0;*(p+i)!='\0';i++)
                       ^
                     This is the null character

同样,

if(*(p+i+1)=='\o')

应该是

if(*(p+i+1)=='\0')

*(p+i+1)='\o';

应该是

*(p+i+1)='\0';

另请注意,您应该使用

int main()
{
  // your code
  return 0;
}

而不是void main()

关于c - 交换 char 指针的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29826515/

相关文章:

C - 两个 for 循环中的数组

python - 使用字符串方法理解代码

java - 如何在二维中使用字符串数组(String[][])并解析每个空间(指定元素点)中的数据?

java - 从 URL 读取 XML 作为字符串

c - 通过指针访问数组时出现段错误

c - 我的 C 代码的输出比我预期的值多 1。为什么?

c++ - 指针变量,var 和 &var 的区别

c - 防止 Frama-C 的切片插件更改输入代码

c - 简化 C 中的嵌套 for 循环

php - SplFixedArray 是在幕后用 C/C++ 实现的吗?