- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基本上我想要做的是,当用户输入一个字符串时,我的代码将一个一个地检查字符,看看它们是否属于一个数组。
For instance i have an array:
char example[] = { 'a', 'b', 'c', 'd', 'e' };
假设用户输入了一个字符串“example string” 现在我想单独检查字符串中的每个字符 如果它们存在于给定数组中。所以第一个字母“e”显然在数组中 而给定数组中不存在字母“x”。到目前为止,我正在尝试 使用循环和 memchr,但由于某种原因它无法正常工作,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char array[] = { 'a', 'b', 'c', 'd', 'e' };
char input[40]; /*Reserved for the string*/
int lengthofstring,i;
scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say
the string in this case is "example"*/
lengthofstring=strlen(input);
for (i=0;i<lengthofstring;i++){
if (memchr(array,input[i],sizeof(array)){
/* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
printf("The letter %c does exist\n",input[i]);
}
else {
printf("The letter %c does NOT exist\n",input[i]);
}
}
}
我真的很难弄清楚这段代码有什么问题,由于某种原因它总是在不存在的类别中结束。非常感谢任何建议或帮助,在此先感谢。
最佳答案
代码似乎对我有用。我确实编辑了问题中的代码,我可能无意中删除了问题。请随时重新编辑并发布原始代码,我似乎无法回滚我的编辑。
工作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char array[] = { 'a', 'b', 'c', 'd', 'e' };
char input[40]; /*Reserved for the string*/
int lengthofstring,i;
scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say
the string in this case is "example"*/
lengthofstring=strlen(input);
for (i=0;i<lengthofstring;i++){
if (memchr(array,input[i],sizeof(array))) {
/* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
printf("The letter %c does exist\n",input[i]);
}
else {
printf("The letter %c does NOT exist\n",input[i]);
}
}
}
我在 if
语句上添加了 2 个结束 }
和一个结束 )
原代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char array[] = { 'a', 'b', 'c', 'd', 'e' };
char input[40]; /*Reserved for the string*/
int lengthofstring,i;
scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say
the string in this case is "example"*/
lengthofstring=strlen(input);
for (i=0;i<lengthofstring;i++){
if (memchr(array,input[i],sizeof(array)){
/* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
printf("The letter %c does exist\n",input[i]);}
else {
printf("The letter %c does NOT exist\n",input[i]);}
关于C- 检查字符串中的字符是否属于数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129070/
如果有一张有客户的 table 和一张有地址的 table 。 一位客户必须(仅)有一个地址,但地址不必有客户(取决于字段类型)。所以我只是问如何告诉cake在寻找客户时获取客户地址,但在寻找地址时不
我有一个问题,我需要获取我的画廊表的所有图像(路径),该表拥有博物馆和拥有博物馆的用户。我得到了图像的路径,但这些与拥有博物馆的 user_id 没有关联。 所以简短的描述: 每个用户拥有一个博物馆,
我有一个问题,我需要获取我的画廊表的所有图像(路径),该表拥有博物馆和拥有博物馆的用户。我得到了图像的路径,但这些与拥有博物馆的 user_id 没有关联。 所以简短的描述: 每个用户拥有一个博物馆,
我有用户和个人资料(一对一属于用户) type User struct { ID int Username string Password string
我想以类似于'belongs to' association的方式使用GORM的Django's one-to-one relationships。考虑以下示例,其中每个User与一个Profile相
长期从事 Rails 开发, Backbone 菜鸟。 在我的 Rails 模型中,一个项目有很多任务,一个任务属于一个项目......标准的东西。 尝试在集合中获取项目的任务 json。 Examp
让我们直奔问题(使用 Grails 1.1.1,它应该适用于以前的问题) 我有 2 个域,即:用户和详细信息,如下所示: Class User { String userName ; ..
我正在尝试在 Rails 中设置模型关系,并且需要您的帮助,因为它不起作用:0 class User :creator_id end 就架构而言,request_threads表具有creator_
注意:我在编写问题时解决了问题,因此不需要答案。仍然与答案分享,以便有相同经历的人可以从中受益。 我有一个回收器 View ,其中包含图像和 2 个 TextView 。我想在单击图像时旋转图像,但发
我正在尝试为我的应用程序中的所有按钮添加自定义点击声音。我已经为 UIButton 创建了一个类类别,其中包含以下代码: NSURL *soundURL = [NSURL fileURLWithPat
我有这样一个IP:12.12.12.12 我正在遍历不同的 IP 范围(12.12.12.0/24(示例))格式,并尝试查看 IP 是否在该范围内。 我尝试了各种方法,例如 inet_addr 和比较
看完这个问题 ASP.NET MVC: Nesting ViewModels within each other, antipattern or no? 和 Derick Bailey 的评论 i t
我正在使用 FeedWordPress 从子公司网页将新闻导入母公司的新闻卷。 可在此处查看特定项目的 RSS 摘录:Pastebin 如您所见,我正在将我需要的图像放入 RSS 文件、描述 bloc
假设我有以下数据库架构: dogs 和 owners 与经典的 belongsToMany 相关联。 walks 表怎么样?我希望能够在这种关系中使用 Eloquent 好东西: $dogs = Do
我不知道出了什么问题,但我无法使用 :class_name 选项让belongs_to 工作。有人可以启发我。非常感谢! 这是我的代码片段。 class CreateUsers false
属于 JavaScript 对象原型(prototype)的回调函数如何访问对象成员?回调不能关闭,一切都必须定义如下: function Obji(param){ this.element =
有人可以解释一下 MarkupCompilePass1 和 PartialClassGenerationTask 这两个构建任务是如何归属在一起的吗?目前我不知道他们是否共存或者是否需要对方。有人可以
现在,我尝试了解 Grails 域类和 GORM 中的工作原理。所以,我尝试实验: 我试验了两个域类:Main 和 Sub。 我们走吧! 第 1 步: class Main { String
所以我有一个模型 App.DailyEntry = DS.Model.extend({ user_id: belongsTo('user'), entries: hasMany('En
我正在使用 primeng 组件选项卡菜单。 https://www.primefaces.org/primeng/#/tabmenu我找不到将所选 TAB 的颜色更改为不同颜色的方法。 最佳答案 抱
我是一名优秀的程序员,十分优秀!