2012-08-06 106 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; 
} 
相關問題