Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中修改和替换字符数组中的值_C_Arrays_String_While Loop - Fatal编程技术网

在C中修改和替换字符数组中的值

在C中修改和替换字符数组中的值,c,arrays,string,while-loop,C,Arrays,String,While Loop,正如程序所显示的,我不知道打印出新结果有什么不对。 e、 g.当我调用完位置为0、值为v的modify_str()函数时, 结果不会出现“vello”,但会抛出一个简短的胡言乱语。 谁能识别出错误!?谢谢:) #包括 作废打印(字符*abc); void modify_str(char*a); void main(){ 字符c[20]=“你好”; 印刷品(c); 修改_str(c); } 无效打印(字符*str){ int i=0; 而(*(str+i)!='\0'){ printf(“%c”,

正如程序所显示的,我不知道打印出新结果有什么不对。 e、 g.当我调用完位置为0、值为v的modify_str()函数时, 结果不会出现“vello”,但会抛出一个简短的胡言乱语。 谁能识别出错误!?谢谢:)

#包括
作废打印(字符*abc);
void modify_str(char*a);
void main(){
字符c[20]=“你好”;
印刷品(c);
修改_str(c);
}
无效打印(字符*str){
int i=0;
而(*(str+i)!='\0'){
printf(“%c”,str[i]);
i++;
}
printf(“\n”);
}
void modify_str(char*c){
字符q[]=“要修改的字符位置(0-n):”;
打印(q);
int pos;
scanf(“%d”和“&pos”);
printf(“替换值:”);
字符w;
扫描频率(“%s”和“w”);
int指数=0;
而(*(c+索引)!='\0'){
如果(索引==位置){
c[指数]=w;
printf(“新结果:%c\n”,c);
}
索引++;
}
}

使用不正确的格式说明符代替此调用

scanf("%s", &w);
使用

while循环应该如下所示

while( *(c+index) !='\0' && index != pos ) ++index;

if ( index == pos && *( c + index ) != '\0' )
{
        c[index]=w;
        printf("New Result: %s\n", c);
                            ^^  
}
另外,用标识符
c
命名字符串也是个坏主意,该标识符通常用于命名
char
类型的对象。最好使用标识符
s

考虑到根据C标准,无参数的主要功能应声明如下

int main( void )
顺便说一句,功能
print\uuu
可以实现如下

void print_( const char *str )
{
    puts( str );
}

并且不要在标识符中使用尾随下划线。这只是一种糟糕的编程风格。

它是C中的
intmain
,而不是
voidmain
。C不是Java。不测试scanf()的返回值总是一个错误。你喜欢
void
。你喜欢
while()
像@BLUEPIXY一样改变它为什么你总是在场外发布你非常好的答案?你会第一个反对场外问题吗?@WeatherVane,因为没有人需要它。但现在我无法实现内部打印新结果声明。可能是条件阻止了打印。@Y.Andy此声明应该在循环之外。没有理由将其放置在while循环中。您可以将printf调用移到if语句之外。干杯!你帮了大忙
int main( void )
void print_( const char *str )
{
    puts( str );
}