假设我有一个字符串"qwerty",我希望在其中找到e字符的索引位置.(在这种情况下,索引将是2)
我怎么用C做?
我找到了strchr函数,但它返回一个指向字符而不是索引的指针.
c中是否有一个函数可以返回char数组中char的索引?
例如:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
int index = findIndexOf( values, find );
Run Code Online (Sandbox Code Playgroud) 我想检查单个字符串是否在C字符串中.该字符是'|'
用于Linux的管道(其实我也想检查'<','>','>>','&').
在Java中我可以这样做:
String.indexOf()
Run Code Online (Sandbox Code Playgroud)
但是如何在C中完成此操作而不循环遍历整个字符串(char*字符串)?
这是一个接受的程序:
如何找到句子中输入单词的位置?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sntnc[50], word[50], *ptr[50];
int pos;
puts("\nEnter a sentence");
gets(sntnc);
fflush(stdin);
puts("\nEnter a word");
gets(word);
fflush(stdin);
ptr=strstr(sntnc,word);
//how do I find out at what position the word occurs in the sentence?
//Following is the required output
printf("The word starts at position #%d", pos);
return 0;
}
Run Code Online (Sandbox Code Playgroud)