任何人都可以帮我处理我的代码
int main(void){
int ctr,wordLength;
char theWord[99];
ctr = 0;
printf("Enter a Word: ");
scanf("%s", & );
printf("Enter the letter you want to find: ");
scanf("%s", & );
while(ctr < wordLength | theWord[ctr]=='a'){
ctr++;
}
//output
}
期待输出
输入Word:hello
输入您要查找的字母:z
在单词中找不到字母z。
答案 0 :(得分:0)
更正后的代码
int main (void)
{
int ctr = 0;
int wordLength;
char theWord[99], ch;
printf("Enter a Word: ");
scanf("%s", theWord);
printf("Enter the letter you want to find: ");
scanf("%c", &ch);
while (theWord [ctr] != '\0')
{
if (theWord [ctr] == ch)
{
print("Character found at position %1", ctr);
break;
}
ctr++;
}
if (theWord [ctr] == '\0')
{
printf ("Character not found");
}
}
答案 1 :(得分:0)
你也可以这样做,
#include <stdio.h>
int main(void)
{
int ctr;
char theWord[99], ch;
ctr = 0;
printf("Enter a Word: ");
scanf("%s", theWord);
printf("Enter the letter you want to find: ");
scanf(" %c", &ch );
for(int i = 0; theWord[i]; i++)
{
if(theWord[i] == ch)
ctr++;
}
if(ctr != 0)
printf("word is found\n");
else
printf("word is not found\n");
}
是的,我们也可以使用while循环来实现它,
int i = 0;
while(theWord[i] != '\0' )
{
if(theWord[i] == ch)
ctr++;
i++;
}
if(ctr != 0)
printf("word is found\n");
else
printf("word is not found\n");
答案 2 :(得分:-1)
我修改了你的代码,它与GCC成功合作
#include<stdio.h>
#include <string.h>
int main(void){
int ctr = 0,c = 0, wordLength;
char ch, theWord[99];
printf("Enter a Word: ");
scanf("%s", theWord);
printf("Enter the letter you want to find: ");
scanf("%c", & ch);
wordLength = strlen(theWord);
while(theWord[ctr] != '\0')
{
if (theWord[ctr] == ch)
{
printf("the letter %c is found in the word\n", ch);
c++;
}
ctr++;
}
if (c == 0 )
{
printf("the letter %c is NOT found in the word\n", ch);
}
}