C语言有关字符替换的问题!分数不多~麻烦大家了~

来源:百度知道 编辑:UC知道 时间:2024/05/28 05:08:49
这是我的程序中的一段子函数,麻烦大家帮我修改下这一段(编译的时候没反应)
TitleRetri() /*search the book information by title*/
{
int count;
count=BookFileInput();
int i=0;
char GoOn;
char typeTitle[100],getTitle[100],Temp[100];
do
{
int j=0,k=0;
printf("Please Input the Title of the Book You Want to Search:\n");
scanf("%s",typeTitle);
gets(Temp);
for(j=0;typeTitle[j]!='\0';j++)
{
if(typeTitle[j]==' ') getTitle[j]='_';
else getTitle[j]=typeTitle[j];
}
for(i=0;i<count-1;i++)
{
if(strcmp(getTitle,book[i].title)==0)
{
printf("\nBook Information as Fo

下面是完整的程序,读入书名,允许有空格,用下划线替换所遇到的空格。结果在char temp[100] 中。

#include "stdio.h"

void main()
{
char temp[100];
int L,i;

printf("please enter book name, space key is allowed\n");

gets(temp); // read the book name until "\n"
L = strlen(temp);

for (i=0;i<L;i++)
if (strncmp(temp+i," ",1) == 0) strncpy(temp+i,"_",1);

printf("%s",temp);
}

例如:
输入 abc def, 输出abc_def
输入1 2 3 4 5, 输出1_2_3_4_5

在原字符串中操作即可以了.

编译没反应是什么意思

一般的用法是:
strchr(str, ch) = newch;
不必自己写查找了。strchr()函数查找单个字符