2012-08-06 84 views
7

这里串的位置是一个程序来接受:从用户在C中找到一个字符串

  1. 句子。从用户
  2. 字。

如何找到在句子中输入的字的位置?

#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; 
} 
+1

可以减去2个指针(至'char')和解释结果为整数:'位置= PTR - sntnc;' – pmg 2012-08-06 21:57:05

+6

** DO NOT使用'gets()'!不要在Java/JavaScript中使用'fflush()'输入流!** – pmg 2012-08-06 21:57:50

+0

我们正是您需要的函数:indexOf。然而快速搜索使我能够找到一个线程讨论你所需要的:像C函数的indexOf一,请查看这篇文章:http://stackoverflow.com/questions/4824/string-indexof-function-in-c – gaspyr 2012-08-06 21:58:54

回答

15

ptr指针将指向word开始,所以你可以只减去了一句指针的位置,sntnc,从中:

pos = ptr - sntnc; 
+0

打败我,党:P – 2012-08-06 22:03:28

+9

...但只有当'ptr'不是'NULL'。 – caf 2012-08-06 22:27:02

4

的strstr的回报()是指向你的“字”的第一次出现,所以

pos=ptr-sntc; 

这只能是因为sntc和PTR都指向相同的字符串。为了澄清当我说出现的时候,它是在目标字符串中找到匹配字符串时第一个匹配字符的位置。

2

仅供参考:

char saux[] = "this is a string, try to search_this here"; 
int dlenstr = strlen(saux); 
if (dlenstr > 0) 
{ 
    char *pfound = strstr(saux, "search_this"); //pointer to the first character found 's' in the string saux 
    if (pfound != NULL) 
    { 
     int dposfound = int (pfound - saux); //saux is already pointing to the first string character 't'. 
    } 
} 
1

对于我与的strstr)麻烦(一些原因,我也想索引。

我做了这个函数查找字符串的一个更大的字符串中的位置(如果存在),否则返回-1。

int isSubstring(char * haystack, char * needle) { 
    int i = 0; 
    int d = 0; 
    if (strlen(haystack) >= strlen(needle)) { 
     for (i = strlen(haystack) - strlen(needle); i >= 0; i--) { 
      int found = 1; //assume we found (wanted to use boolean) 
      for (d = 0; d < strlen(needle); d++) { 
       if (haystack[i + d] != needle[d]) { 
        found = 0; 
        break; 
       } 
      } 
      if (found == 1) { 
       return i; 
      } 
     } 
     return -1; 
    } else { 
     //fprintf(stdout, "haystack smaller\n"); 
    } 
} 
0

我在这个线程原来的职位评论: 该声明是错误的:

char sntnc[50], word[50], *ptr[50]; 

C代码甚至不会编译:它将无法在这一行:

ptr = strstr(sntnc,word); 

所以行应改为:

char sntnc[50], word[50], *ptr; 

而且你不需要分配给“PTR字符串” memeory。你只需要一个指向char的指针。

0

可以使用这种简单的变形例strpos

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
int strpos(char *haystack, char *needle, int offset); 
int main() 
{ 
    char *p = "Hello there all y'al, hope that you are all well"; 
    int pos = strpos(p, "all", 0); 
    printf("First all at : %d\n", pos); 
    pos = strpos(p, "all", 10); 
    printf("Second all at : %d\n", pos); 
} 


int strpos(char *hay, char *needle, int offset) 
{ 
    char haystack[strlen(hay)]; 
    strncpy(haystack, hay+offset, strlen(hay)-offset); 
    char *p = strstr(haystack, needle); 
    if (p) 
     return p - haystack+offset; 
    return -1; 
} 
相关问题