在C中替换字符串中的字符
必须用另一个用户输入字符替换用户输入字符并打印字符串。我做错了什么在C中替换字符串中的字符,c,C,必须用另一个用户输入字符替换用户输入字符并打印字符串。我做错了什么 #include<stdio.h> #include<conio.h> main() { int i; char a,b,str[100]; printf("Enter the string"); gets(str); //asking for replacement printf("enter the character to be replaced"); scanf("%c",&a); //
#include<stdio.h>
#include<conio.h>
main()
{
int i;
char a,b,str[100];
printf("Enter the string");
gets(str);
//asking for replacement
printf("enter the character to be replaced");
scanf("%c",&a);
// which letter to replace the existing one
printf("enter the character to replace");
scanf("%c",&b);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
str[i] = b;
}
else
continue;
}
printf("the new string is");
puts(str);
}
#包括
#包括
main()
{
int i;
字符a,b,str[100];
printf(“输入字符串”);
获取(str);
//要求更换
printf(“输入要替换的字符”);
scanf(“%c”和“&a”);
//用哪个字母替换现有的
printf(“输入要替换的字符”);
scanf(“%c”和“b”);
对于(i=0;str[i]!='\0';i++)
{
如果(str[i]==a)
{
str[i]=b;
}
其他的
继续;
}
printf(“新字符串为”);
put(str);
}
你得到一个整数了吗?不是角色?如果是字符,则应使用%c
而不是%d
在两个scanf()之间添加getchar()
函数
像
但是在删除这个getchar()
之前,试试这个,它对我很有用:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
char a,b,str[100];
printf("Enter the string: ");
gets(str);
//asking for replacement
printf("enter the character to be replaced: ");
a = _getch();
printf("\n%c", a);
// which letter to replace the existing one
printf("\nenter the character to replace: ");
b = _getch();
printf("\n%c", b);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
str[i] = b;
}
else
continue;
}
printf("\nthe new string is: ");
puts(str);
}
#包括
#包括
main()
{
int i;
字符a,b,str[100];
printf(“输入字符串:”);
获取(str);
//要求更换
printf(“输入要替换的字符:”);
a=_getch();
printf(“\n%c”,a);
//用哪个字母替换现有的
printf(“\n输入要替换的字符:”;
b=_getch();
printf(“\n%c”,b);
对于(i=0;str[i]!='\0';i++)
{
如果(str[i]==a)
{
str[i]=b;
}
其他的
继续;
}
printf(“\n新字符串为:”);
put(str);
}
您可以删除else
块。它不会影响任何事情。#包括
#include<stdio.h>
#include<conio.h>
int main() //main returns an int
{
int i;
char a,b,str[100];
printf("Enter the string\n");
fgets(str,sizeof(str),stdin);//gets is dangerous
printf("Enter the character to be replaced\n");
scanf(" %c",&a); //space before %c is not neccessary here
printf("Enter the character to replace\n");
scanf(" %c",&b); //space before %c is compulsory here
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
str[i] = b;
}
//else //This part is not neccessary
//continue;
}
printf("The new string is ");
puts(str);
return 0; //main returns an int
}
#包括
int main()//main返回一个int
{
int i;
字符a,b,str[100];
printf(“输入字符串\n”);
fgets(str,sizeof(str),stdin);//get是危险的
printf(“输入要替换的字符\n”);
scanf(“%c”,&a);//此处不需要%c之前的空格
printf(“输入要替换的字符\n”);
scanf(“%c”,&b);//此处必须使用%c之前的空格
对于(i=0;str[i]!='\0';i++)
{
如果(str[i]==a)
{
str[i]=b;
}
//否则//此部分不是必需的
//继续;
}
printf(“新字符串为”);
put(str);
返回0;//main返回一个int
}
我用了,因为它不能阻止
最重要的是跳过空格,即空格、新行等,在第一个scanf
中不需要它,因为fgets
也使用新行字符并将其放入缓冲区
else继续的原因代码>是不需要的,因为循环将在到达循环体末端时检查条件
我使用了int main()
和返回0
,因为根据最新标准
最后,您的程序中还有一个未使用的头conio.h
。建议:使用fgets()
而不是gets()
。注意:将secondscanf()
更改为scanf(%c,&b)代码>注意:否则继续代码>。。有点多余。可能是重复的。非常感谢。但我仍然没有得到正确的输出。还有没有办法继续使用scanf并解决换行符作为角色的问题?
scanf(" %c",&b);
#include<stdio.h>
#include<conio.h>
main()
{
int i;
char a,b,str[100];
printf("Enter the string: ");
gets(str);
//asking for replacement
printf("enter the character to be replaced: ");
a = _getch();
printf("\n%c", a);
// which letter to replace the existing one
printf("\nenter the character to replace: ");
b = _getch();
printf("\n%c", b);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
str[i] = b;
}
else
continue;
}
printf("\nthe new string is: ");
puts(str);
}
#include<stdio.h>
#include<conio.h>
int main() //main returns an int
{
int i;
char a,b,str[100];
printf("Enter the string\n");
fgets(str,sizeof(str),stdin);//gets is dangerous
printf("Enter the character to be replaced\n");
scanf(" %c",&a); //space before %c is not neccessary here
printf("Enter the character to replace\n");
scanf(" %c",&b); //space before %c is compulsory here
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
str[i] = b;
}
//else //This part is not neccessary
//continue;
}
printf("The new string is ");
puts(str);
return 0; //main returns an int
}