给定如下的数组:
char chars[] = { 'a', ' ', 'b', ' ', 'c', ' ', ' ', 'd' } ;
写一个程序将数组中所有的空格字符替换为下划线字符'_'。
使用指针而不是下标访问数组元素。
**输出格式要求:"替换后数组为:" "%c "
#include <stdio.h>
#include <stdlib.h>
int main()
{
char chars[] = {'a', ' ', 'b', ' ', 'c', ' ', ' ', 'd'};
char *p;
p = chars;
while (p < chars + (sizeof(chars) / sizeof(char)))
{
if (*p == ' ')
{
*p = '_';
}
p++;
}
printf("替换后数组为:");
p = chars;
while (p < chars + (sizeof(chars) / sizeof(char)))
{
printf("%c ", *p);
p++;
}
return 0;
}