- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include <stdio.h>
#include <string.h>
int main()
{
char a[250];
char c1[1],c2[1];
int n,i;
printf("Give text: ");
gets(a);
printf("Give c1: ");
gets(c1);
printf("Give c2: ");
gets(c2);
n=strlen(a);
for(i=0;i<n;i++)
{
if(a[i]==c1)
{
a[i]=c2;
}
if(a[i]==c2)
{
a[i]=c1;
}
}
printf("%s",a);
return 0;
}
在文本中,我需要将 c1
切换为 c2
并反转,但是当我在给出 a、c1
、c2
后启动程序时,什么也没有发生。我哪里错了?
最佳答案
首先,don't use gets()
, it's inherently dangerous , 使用 fgets()
相反。
最重要的是,当您使用 gets(c1)
时,c1
是一个单元素数组,您已经超出了调用 undefined behavior 的已分配内存。 .
就是说,您将 c1
和 c2
作为单元素数组,这并没有错,但也不是必需的。将它们定义为简单的 char
变量
char c1;
char c2;
像这样使用它们
scanf(" %c", &c1); // mind the space and don't forget to to check the return
scanf(" %c", &c2); // value of scanf() to ensure proper scanning.
之后,a[i] == c2
的检查应该作为 else
构造,否则,您将覆盖之前的操作。有点像
for(i=0;i<n;i++)
{
if(a[i]==c1)
{
a[i]=c2;
}
else if(a[i]==c2)
{
a[i]=c1;
}
}
关于迷惑C开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632429/
我有以下工厂。它还有一个返回允许时间数组的函数(根据调用返回的信息计算)。我想在模板中提供此信息,我该如何在 Controller 中使用? .factory('Company', function(
我需要访问一些网页并像浏览器一样传递 cookie。这很容易使用 CookieHandler.setDefault(new MyCookieManager()); 但这引入了我需要避免的全局状态(想象
当我从 Rails 工作室搬到 Java 商店时,我主要是一名 JS 开发人员,正在学习 Maven。 Codehaus 的人员在提供 Maven 原型(prototype)方面提供了巨大帮助,以指导
从 learningpythonthehardway 开始,我已经进入了文字冒险游戏,我有一些代码,我理解它们执行的内容,但我不明白如何执行。下面是我缩小到单个 .py 文件中的整个代码: from
我在找 here在 CSS 中:事件选择器。 The :active selector styles links toactive pages 这让我开始思考,HTML/CSS 术语中的“事件页面”到
R具有qr()函数,该函数使用LINPACK或LAPACK执行QR分解(以我的经验,后者快5%)。返回的主要对象是一个矩阵“ qr”,该矩阵包含在上三角矩阵R中(即R=qr[upper.tri(qr)
我是一名优秀的程序员,十分优秀!