力任せ法による文字列探索

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
まさる

力任せ法による文字列探索

#1

投稿記事 by まさる » 2年前

C言語についての質問です。力任せ法の文字列探索を行うプログラムを作成したいのですが、実行するとRuntime errorとなり結果が反映されません。参考書を見てもどのコード間違っているのか分からなかったため質問をさせていただきました。


コード:

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

char *bf_match(char *pat , char *txt){
char *pt; /* txt をなぞるカーソル */
char *pp; /* pat をなぞるカーソル */
int txt_len = strlen(txt); /* txt の文字数 */
int pat_len = strlen(pat); /* pat の文字数 */


pt = txt;
pp = pat;

while(*pt != '\0' && *pp != '\0'){
if(*pt == *pp){
pt++;
pp++;
}else{
pt= pt - pp + 1;
pp = 0;
}
}
if(*pp == '\0'){
return pt - pp;
}
return NULL;
}



int main(void){
char *s;
char s1[80]; /* テキスト */
char s2[80]; /* パターン */
printf("テキスト:");
scanf("%s", s1);
printf("パターン:");
scanf("%s", s2);
s = bf_match(s2, s1);
if (s == NULL)
puts("テキスト中にパターンは存在しません。");
else
printf("%d 文字目に見つかりました。\n", s+1);
return 0;
}

Build Errorは以下が表示されました。

コード:

Main.c:22:12: warning: incompatible integer to pointer conversion assigning to 'char *' from 'long' [-Wint-conversion]
         pt= pt - pp + 1;
           ^ ~~~~~~~~~~~
Main.c:28:13: warning: incompatible integer to pointer conversion returning 'long' from a function with result type 'char *' [-Wint-conversion]
     return pt - pp;
            ^~~~~~~
Main.c:8:6: warning: unused variable 'txt_len' [-Wunused-variable]
 int txt_len = strlen(txt); /* txt の文字数 */
     ^
Main.c:9:6: warning: unused variable 'pat_len' [-Wunused-variable]
 int pat_len = strlen(pat); /* pat の文字数 */
     ^
Main.c:10:6: warning: unused variable 'skip' [-Wunused-variable]
 int skip[UCHAR_MAX + 1]; /* スキップテーブル */
     ^
Main.c:11:6: warning: unused variable 'i' [-Wunused-variable]
 int i;
     ^
Main.c:6:8: warning: variable 'pt' is used uninitialized whenever function 'bf_match' is called [-Wsometimes-uninitialized]
 char *pt; /* txt をなぞるカーソル */
 ~~~~~~^~
Main.c:15:9: note: uninitialized use occurs here
 while(*pt != '\0' && *pp != '\0'){
        ^~
Main.c:6:10: note: initialize the variable 'pt' to silence this warning
 char *pt; /* txt をなぞるカーソル */
         ^
          = NULL
Main.c:49:58: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
     printf("%d 文字目に見つかりました。\n", s+1);
             ~~                              ^~~
             %s
8 warnings generated.

アバター
あたっしゅ
記事: 663
登録日時: 13年前
住所: 東京23区
連絡を取る:

Re: 力任せ法による文字列探索

#2

投稿記事 by あたっしゅ » 2年前

東上☆海美☆「
参考書の名前を書いてくださいみみ。

あと、参考書の他に、元のプログラムを書くうえで参考にした 資料 ? サイト ? があるみみ。
その資料の名前を書いてみみ。

で、

Runtime error

なんでしょうみみ ?

Build Error

なんでしょうみみ ?
ソースコードを VS2019 でビルドしてみたところ、Build Error のようですがみみ。

コード:

//
// https://dixq.net/forum/viewtopic.php?f=3&t=21151&sid=cfe85752ca3b260584f80a0f563f4c04
// 力任せ法による文字列探索 - ミクプラ(ja)
//
#include <stdio.h>
#include <string.h>


char*
bf_match(char* pat, char* txt) {
	char* pt; /* txt をなぞるカーソル */
	char* pp; /* pat をなぞるカーソル */
	//int txt_len = strlen(txt); /* txt の文字数 */
	//int pat_len = strlen(pat); /* pat の文字数 */

	pt = txt;
	pp = pat;

	while (*pt != '\0' && *pp != '\0') {
		if (*pt == *pp) {
			pt++;
			pp++;
		}
		else {
			pt = pt - pp + 1;
			pp = 0;
		}
	}
	if (*pp == '\0') {
		return pt - pp;
	}
	return NULL;
}



int
main(void) {
	char* s;
	char s1[80]; /* テキスト */
	char s2[80]; /* パターン */

	printf("テキスト:");
	scanf("%s", s1);
	printf("パターン:");
	scanf("%s", s2);
	s = bf_match(s2, s1);
	if (s == NULL)
		puts("テキスト中にパターンは存在しません。");
	else
		printf("%d 文字目に見つかりました。\n", s + 1);
	return 0;
}


// end.
検索したところ
http://www.bohyoh.com/Books/CalgoA/EX/ALGOEX0809.html
が出てきました。
VTuber:
東上☆海美☆(とうじょう・うみみ)
http://atassyu.php.xdomain.jp/vtuber/index.html
レスがついていないものを優先して、レスするみみ。時々、見当外れなレスしみみ。

中の人:
手提鞄あたッしュ、[MrAtassyu] 手提鞄屋魚有店
http://ameblo.jp/mratassyu/
Pixiv: 666303
Windows, Mac, Linux, Haiku, Raspbery Pi, Jetson Nano, 電子ブロック 持ち。

返信

“C言語何でも質問掲示板” へ戻る