关于c语言 在线等

来源:百度知道 编辑:UC知道 时间:2024/05/27 00:53:55
#include <stdio.h>
#include <string.h>
void fun (char *str,char ch)
{
while (*str &&*str !=ch) str++;
if(*str!=ch)
{ str[0]=ch;
str[1]=0;
}
}
void main()
{char s[81],c;
system("CLS");
printf("\nPlease enter a string : ");
gets(s);
printf("\n Please enter the character to search : ");
c=getchar();
fun(s,c);
printf("\nThe result is %s\n",s);
}

str[1]=0是什么意思?

就是str这个字符数组的第二个字符设置为空,表示字符串到此结束了
看那个fun函数的功能,是把第二个参数传给它的字符加到第一个参数字符串的后面(如果该字符不在字符串内出现过)

while (*str &&*str !=ch) str++;
//查找 str有内容并且内容!=ch 时,str++

if(*str!=ch)
{ str[0]=ch;
str[1]=0;
}
//if(*str!=ch) 说明没有查找到 ch 字符(不包含)
//str[0] 中止str字符串,字符串到此结束

这个程序是查找相同的字符
printf("\nThe result is %s\n",s);
输出的时候是以‘\0’结束的,在函数中找出要找的字符后在其后加一个
0 就知道字符串是在什么时候结束了 比如你输入abcdef查找c实际
s中保存的就是c\0cdef\0在输出的时候就以0结束 只输出c

你写详细点