交换 char 指针的值
Exchanging the values of char pointers
我正在尝试一些事情...但我无法理解结果
#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
与 [=18=]
不同。 \o
将被视为 o
。 Reference
for(int i=0;*(p+i)!='\o';i++)
应该是
for(int i=0;*(p+i)!='[=11=]';i++)
^
This is the null character
同样,
if(*(p+i+1)=='\o')
应该是
if(*(p+i+1)=='[=13=]')
和
*(p+i+1)='\o';
应该是
*(p+i+1)='[=15=]';
另请注意,您应该使用
int main()
{
// your code
return 0;
}
而不是void main()
抓住! :)
#include <stdio.h>
#include <string.h>
#include <cctype.h>
char * remove_vowels( char *s )
{
const char *vowels = "aeiou";
char *q = s;
while ( *q && !strchr( vowels, tolower( *q ) ) ) ++q;
char *p = q;
while ( *q )
{
if ( !*++q || !strchr( vowels, tolower( *q ) ) ) *p++ = *q;
}
return s;
}
int main( void )
{
char s[] = "abcdefghijklmnopqrstuvwxyz";
puts( s );
puts( remove_vowels( s ) );
return 0;
}
程序输出为
abcdefghijklmnopqrstuvwxyz
bcdfghjklmnpqrstvwxyz
由于您打错了字并写了 '\o'
而不是 '[=13=]'
我决定从函数中完全排除这个转义字符。:)
我正在尝试一些事情...但我无法理解结果
#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
与 [=18=]
不同。 \o
将被视为 o
。 Reference
for(int i=0;*(p+i)!='\o';i++)
应该是
for(int i=0;*(p+i)!='[=11=]';i++)
^
This is the null character
同样,
if(*(p+i+1)=='\o')
应该是
if(*(p+i+1)=='[=13=]')
和
*(p+i+1)='\o';
应该是
*(p+i+1)='[=15=]';
另请注意,您应该使用
int main()
{
// your code
return 0;
}
而不是void main()
抓住! :)
#include <stdio.h>
#include <string.h>
#include <cctype.h>
char * remove_vowels( char *s )
{
const char *vowels = "aeiou";
char *q = s;
while ( *q && !strchr( vowels, tolower( *q ) ) ) ++q;
char *p = q;
while ( *q )
{
if ( !*++q || !strchr( vowels, tolower( *q ) ) ) *p++ = *q;
}
return s;
}
int main( void )
{
char s[] = "abcdefghijklmnopqrstuvwxyz";
puts( s );
puts( remove_vowels( s ) );
return 0;
}
程序输出为
abcdefghijklmnopqrstuvwxyz
bcdfghjklmnpqrstvwxyz
由于您打错了字并写了 '\o'
而不是 '[=13=]'
我决定从函数中完全排除这个转义字符。:)