1

文字列の一部を取得しようとしています。
次のコードがあります。

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

char mystring[]="The quick brown fox jumps over the lazy dog";
char word1[]="The";
char * posb,pose;
char * word2;
int b,e,n;

n=memcmp(mystring, word1, sizeof(word1)-1);
if (n==0) printf("the first word is found!\n");
posb=strchr(mystring,' ');   // posb will be a pointer to the first character
b=posb-mystring+1;
printf("posb -> %d\n",posb);
printf("b -> %d\n",b);
pose=strchr(mystring+b+1,' ');   // pose will be a pointer to the second character
printf("calc e\n");
e=pose-sizeof(mystring)+1;
printf("pose -> %s\n",pose);
printf("e -> %d\n",e);
word2 = (char*) malloc (sizeof(char)*(e-b));
memcpy(word2, posb, sizeof(word2));
printf("%s:%i:%i:%i\n",word2, b, e, e-b);
free (word2);

問題は、2 番目の単語を取得して word2 に格納することです。このために、使用strchrしてスペースを見つけようとします。しかし、2 回目に使用するときstrchrは、2 番目のスペースを見つけるためにオフセットが必要です。私は次のことを試しました:

pose=strchr(mystring+b+1,' ');
pose=strchr(&mystring[b+1],' ');

変数beには、 の空白文字の位置が含まれている必要がありますmystring。最終的にword2含む必要があります。 別の解決策は、ループを使用して文字列を「ウォークスルー」することですが、それは関数をごまかすことになります。quick
strchr

4

2 に答える 2

1

2番目の単語を取得するには、これを確認してください

char mystring[] = "The quick brown fox jumps over the lazy dog";
char word1[] = "The";
char * posb, pose;
char * word2;
int b, e, n;

n = memcmp(mystring, word1, sizeof(word1)-1);
if (n == 0) printf("the first word is found!\n");
posb = strchr(mystring, ' ');   // posb will be a pointer to the first character

b = posb - mystring + 1;

printf("posb -> %d\n", posb);
printf("b -> %d\n", b);
posb = strchr(posb + 1, ' ');   // pose will be a pointer to the second character
printf("calc e\n");

e = posb - mystring + 1;
printf("e -> %d\n", e);

word2 = (char*)malloc(sizeof(char)*(e - b));
memcpy(word2, mystring + b, e - b);
word2[e-b-1] = '\0';
printf("%s:%i:%i:%i\n", word2, b, e, e - b);
free(word2);
于 2015-12-23T11:58:29.987 に答える
0

行内で単語の出現を検索する場合、行内の各文字を進めるためのポインター、検索語、現在の文字と検索語の最初の文字の比較、一致する場合のさらなる比較、および次に、必要に応じて比較を制限する区切り文字のセット。たとえば、一致のみを検索し、、、、などを検索しない場合...'the''the''they''them''then''there'

基本的な算術演算を有利に使用して、検索語の長さなどよりも短い行をチェックしようとしないようにすることができます。短い例 (例の目的で行の頭文字'The'をに変更します)、次のようなことを行うことができます。'the'以下。検索で使用される基本的な最適化のみがあり、網羅的な例を意図したものではないことに注意してください。

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

int main (int argc, char **argv)
{
    char line[] = "the quick brown fox jumps over the lazy dog.";
    char *p = line;
    char *sterm = argc > 1 ? argv[1] : "the";
    char *delim = " \t\n\'\".";
    size_t llen = strlen (line);
    size_t count = 0, slen = strlen (sterm);

    printf ("\n searching line for '%s'\n\n", sterm);

    for (;p < (line + llen - slen + 1); p++) {

        if (*p != *sterm)
            continue;   /* char != first char in sterm  */
        if (p > line && !strchr (delim, *(p - 1)))
            continue;   /* prior char is not a delim    */
        if (!strchr (delim, *(p + slen)))
            continue;   /* next char is not a delim     */
        if (strncmp (p, sterm, slen))
            continue;   /* chars don't match sterm      */

        printf ("   match %2zu. '%s' at location %zu\n",
                ++count, sterm, p - line);
    }

    printf ("\n total occurrences of '%s' in line : %zu\n\n",
            sterm, count);

    return 0;
}

使用・出力

$ ./bin/searchline

 searching line for 'the'

   match  1. 'the' at location 0
   match  2. 'the' at location 31

 total occurrences of 'the' in line : 2

$ ./bin/searchline fox

 searching line for 'fox'

   match  1. 'fox' at location 16

 total occurrences of 'fox' in line : 1

現在の文字を として使用するstrchr区切り文字列で を使用することに注意してください。例を見て、質問があればお知らせください。あなたの正確な目標はあなたの質問から少し不明確だったので、私があなたの意図を逃した場合はお知らせください.delimchar

ポインターの代わりに配列インデックスを使用する

ポインターの代わりに文字配列インデックスを使用する方が快適な場合は、いつでもポインターを削除して、pそれに応じてループ ロジックを調整できます。

    for (i = 0; i < (llen - slen + 1); i++) {

        if (line[i] != *sterm)
            continue;   /* char != first char in sterm  */
        if (i && !strchr (delim, line[i-1]))
            continue;   /* prior char is not a delim    */
        if (!strchr (delim, line[i+slen]))
            continue;   /* next char is not a delim     */
        if (strncmp (&line[i], sterm, slen))
            continue;   /* chars don't match sterm      */

        printf ("   match %2zu. '%s' at location %zu\n",
                ++count, sterm, &line[i] - line);
    }
于 2016-01-30T21:17:58.633 に答える