Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何找到某个字母在句子中的位置?(c)方案编制_C_Arrays_Text_Warnings_Editing - Fatal编程技术网

我如何找到某个字母在句子中的位置?(c)方案编制

我如何找到某个字母在句子中的位置?(c)方案编制,c,arrays,text,warnings,editing,C,Arrays,Text,Warnings,Editing,我试图找出一个数字在c中的句子中的位置。我对编程有点陌生,我不知道为什么我的代码不起作用 我一直收到这个警告,但我不知道它是什么意思(英语不是我的第一语言): 传递'strcmp'的参数1会从整数生成指针,而不使用强制转换[-Wint conversion]Main.c/TweeIntegers第20行c/c++问题 我的代码: #include <stdio.h> #include <string.h> int main() { int i, y; c

我试图找出一个数字在c中的句子中的位置。我对编程有点陌生,我不知道为什么我的代码不起作用

我一直收到这个警告,但我不知道它是什么意思(英语不是我的第一语言):

传递'strcmp'的参数1会从整数生成指针,而不使用强制转换[-Wint conversion]Main.c/TweeIntegers第20行c/c++问题

我的代码:

#include <stdio.h>
#include <string.h>

int main()
{
    int i, y;
    char x;

    char text1[] = "een stuk text";
    char text2[] = "k";

    for ( i = 0; i < strlen(text1); i++ )
    {
        x = text1[i];
        y = strcmp( x, text2 )
    }

    printf("%d", i);

    return 0;
}
#包括
#包括
int main()
{
int i,y;
字符x;
char text1[]=“een stuk text”;
字符text2[]=“k”;
对于(i=0;i
如果只查找字符和第一个位置,则可以使用以下代码:

#include <stdio.h>
#include <string.h>

int main()
{
  int i;

  char text1[] = "een stuk text";
  char charYouLookFor = 'k';

  for ( i = 0; i < strlen(text1); i++ )
  {
    if (text1[i] == charYouLookFor)
       break;
  }

  printf("%d", i);

  return 0;
}
#包括
#包括
int main()
{
int i;
char text1[]=“een stuk text”;
char charYouLookFor='k';
对于(i=0;i

如果要查找文本在文本中的位置或字符的第二个位置,则代码需要更复杂。

您试图将单个字符与字符串进行比较,但
strcmp()
比较两个字符串。您可以通过删除整个循环来解决这个问题,只需使用
strhr()
来定位角色
strstr(text1,text2)
也可以工作,因为text2是一个字符串(正确地以null结尾)

string.h中预先制作的搜索函数:

  • strchr()
    在字符串中查找单个字符
  • strstr()
    在另一个字符串中查找子字符串
  • strpbrk()
    从另一个字符串中的指定字符列表中查找任何字符

您只能使用
strcmp()
来比较整个字符串,这不是您想要做的

要定位字符串中的单个字符,只需使用。无需自我循环:

#include <stdio.h>
#include <string.h>

int main()
{
    int i, y;
    char x;

    const char text[] = "een stuk text";
    char letter = 'k';

    const char * const found = strchr(text, letter);
    if(found != 0)
      printf("%d\n", (int) (found - text));

    return 0;
}

这是正确的,它是第8个字母。

strcspn将搜索字符列表并返回第一个匹配的索引。在这种情况下,列表仅为字母
k
。如果未找到匹配项,则返回搜索字符串的长度

#include <stdio.h>
#include <string.h>

int main()
{
    int y = 0;
    char text1[] = "een stuk text";
    char text2[] = "k";

    y = strcspn ( text1, text2);

    printf("%d", y);

    return 0;
}
#包括
#包括
int main()
{
int y=0;
char text1[]=“een stuk text”;
字符text2[]=“k”;
y=strcspn(text1,text2);
printf(“%d”,y);
返回0;
}

变量
x
是一个单独的
char
,该函数比较字符串(即以零结尾的字符数组)。您将一个
char
(即整数)传递给一个接受两个
char*
(即字符串)的函数。对于一般情况(查找子字符串),请尝试
char charYouLookFor=“k”
#include <stdio.h>
#include <string.h>

int main()
{
    int y = 0;
    char text1[] = "een stuk text";
    char text2[] = "k";

    y = strcspn ( text1, text2);

    printf("%d", y);

    return 0;
}