#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(){
setlocale(LC_ALL, "Turkish");
char signs[7][6] = {{'A', 'G', 'L', 'S', 'Z', '6'},
{'B', '?', 'M', '?', '0', '7'},
{'C', 'H', 'N', 'T', '1', '8'},
{'?', 'I', 'O', 'U', '2', '9'},
{'D', '?', '?', 'ü', '3', '?'},
{'E', 'J', 'P', 'V', '4', '!'},
{'F', 'K', 'R', 'Y', '5', ' '}};
char str[10] = {'H', 'E', 'L', 'L', 'O', '2', '3'};
int i,j,c=0,flag=0;
for(i=0; i<7; i ){
for(j=0; j<6; j ){
if(signs[i][j] == str[c]){
printf("%c is present at (%d, %d) times in array\n",str[c],i 1,j 1);
flag=1;
break;
}
c ;
}
}
我是一個新的 C 學習者,想在符號[][] 陣列中找到 str[] 的元素,但它不起作用。我該怎么做才能修復它?我的意思是如何在二維陣列中搜索字串的元素?
uj5u.com熱心網友回復:
像?utf-8 這樣的東西,長度是多個字符。因此,它們是字串。你不能使用'?'.
所以,你需要:"?".
使用(例如):
char *signs[] = {"B", "?", "M", "?", "0", "7"};
您還必須將它們作為字串進行比較(例如使用strcmp等)。
這是重構后的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#define COUNTOF(_arr) (sizeof(_arr) / sizeof(_arr[0]))
int
main(void)
{
setlocale(LC_ALL, "Turkish");
#if 0
char signs[7][6] = {
{ 'A', 'G', 'L', 'S', 'Z', '6' },
{ 'B', '?', 'M', '?', '0', '7' },
{ 'C', 'H', 'N', 'T', '1', '8' },
{ '?', 'I', 'O', 'U', '2', '9' },
{ 'D', '?', '?', 'ü', '3', '?' },
{ 'E', 'J', 'P', 'V', '4', '!' },
{ 'F', 'K', 'R', 'Y', '5', ' ' }
};
#endif
#if 0
char *signs[] = {
{ "A", "G", "L", "S", "Z", "6" },
{ "B", "?", "M", "?", "0", "7" },
{ "C", "H", "N", "T", "1", "8" },
{ "?", "I", "O", "U", "2", "9" },
{ "D", "?", "?", "ü", "3", "?" },
{ "E", "J", "P", "V", "4", "!" },
{ "F", "K", "R", "Y", "5", " " }
};
#endif
#if 1
char *signs[] = {
"A", "G", "L", "S", "Z", "6",
"B", "?", "M", "?", "0", "7",
"C", "H", "N", "T", "1", "8",
"?", "I", "O", "U", "2", "9",
"D", "?", "?", "ü", "3", "?",
"E", "J", "P", "V", "4", "!",
"F", "K", "R", "Y", "5", " "
};
#endif
char str[] = "HELLO230?Q?9";
int i, j;
int slen = strlen(str);
for (i = 0; i < COUNTOF(signs); i ) {
char *sign = signs[i];
int signlen = strlen(sign);
int count = 0;
for (j = 0; j < (slen - signlen); j ) {
if (strncmp(sign,&str[j],signlen) == 0) {
count;
printf("%s is present at str[%d] %d times\n",sign,j,count);
}
}
}
return 0;
}
這是程式輸出:
L is present at str[2] 1 times
L is present at str[3] 2 times
? is present at str[8] 1 times
? is present at str[11] 1 times
0 is present at str[7] 1 times
H is present at str[0] 1 times
O is present at str[4] 1 times
2 is present at str[5] 1 times
3 is present at str[6] 1 times
E is present at str[1] 1 times
更新:
它們也可以是寬字符或整數而不是字串嗎?– 用戶 16217248
不,有幾個原因:
- utf-8 字串是 big-endian,但 multichar
char常量實際上是(例如)int并且是 native-endian - 字串中的utf-8 代碼點是可變長度的(例如 1-4 位元組)
- utf-8 更容易處理。可以使用標準函式來處理它們
str*(如上例所示)。 - “寬”字符 [主要] 是 MS/WinX 的東西 [損害了世界其他地方 ;-)]。它們可能不受所有編譯器/庫的支持。
- [AFAIK——我可能錯了] MS 寬字符的編碼與 utf-8 代碼點不同。而且,它們具有不同的多點轉義碼。
我使用的參考資料:
- https://en.wikipedia.org/wiki/UTF-8
- https://en.wikipedia.org/wiki/Wide_character
這是一個獲取字串中 utf-8 代碼點長度的函式:
// utf8len -- get utf-8 codepoint length
int
utf8len(const char *str)
{
unsigned char chr;
int len = 0;
do {
// get starting char
chr = *str ;
// end of string (i.e. string is empty)
if (chr == 0)
break;
// we have at least one byte in the string
len;
// ordinary ASCII -- not utf-8
if ((chr & 0x80) == 0)
break;
// look for end of codepoint:
// (1) ascii char
// (2) start of new codepoint
for (chr = *str ; chr != 0; chr = *str ) {
// (1) char is ASCII (prior char was end of codepoint)
if ((chr & 0x80) == 0)
break;
// (2) char is start of _new_ codepoint
if ((chr & 0xC0) == 0xC0)
break;
// advance codepoint length
len;
}
} while (0);
return len;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/510090.html
標籤:数组C算法搜索多维数组
上一篇:確定一系列連續(正)整數是否可以寫為兩個(正)復合相對素數之和的函式
下一篇:如何根據日期合并排序?
