當前位置:編程學習大全網 - 編程軟體 - 用c語言編壹個查重程序

用c語言編壹個查重程序

#include <stdio.h>

int lookup(char *str, char *key);

main()

{

char str[1024] = "asdfjad asdfh adsf adsf adsf adf adsfasdfkjasdf andsf akdf sdkf"

char key[32];

int pos[100];

printf("輸入要查詢的單詞: ");

scanf("%s", key);

lookup(str, key, pos);

printf("***重復了%d次\n,位置分別是:", pos[0]);

for(i = 1 ; i <= pos[0]; i ++) {

printf("%d “, pos[i]);

}

printf("\n");

}

int lookup(char *str, char *key, int pos[])

{

int i, j;

pos[0] = 0; /* 記錄重復次數 */

for(i = 0; i < strlen(str); i ++) {

if(key[0] == str[i]) {

for(j = 1; j < strlen(key); j ++) {

if(key[j] != str[i+j])

break;

}

if(j == strlen(key)) { /* found */

pos[0] ++;

pos[pos[0]] = i;

i += j-1;

}

}

}

}

  • 上一篇:如何用C語言制作圖形界面?
  • 下一篇:上位機用什麽軟件開發
  • copyright 2024編程學習大全網