Home > OS >  Searching letters in the two dimensional array in C
Searching letters in the two dimensional array in C

Time:09-29

#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  ;
    }
}

I am a new C learner and want to find to the str[]'s elements in the signs[][] array but it does not work. What sould I do to fix it? I mean how can I search a string's element in a two-dimensional array?

CodePudding user response:

Things like Ğ are utf-8 and are multiple chars in length. Thus, they are strings. You can't use 'Ğ'.

So, you need: "Ğ".

Use (e.g):

char *signs[] = {"B", "Ğ", "M", "Ş", "0", "7"};

You'll have to compare them as strings as well (e.g. use strcmp et. al.).


Here is the refactored code:

#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;
}

Here is the program output:

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

UPDATE:

Could they also be wide chars or ints instead of strings? – user16217248

No, for a few reasons:

  1. utf-8 strings are big-endian, but multichar char constants are actually (e.g.) int and are native-endian
  2. utf-8 codepoints in strings are variable length (e.g. 1-4 bytes)
  3. utf-8 is much easier to handle. They can be dealt with using the standard str* functions (as seen in the example above).
  4. "Wide" chars are [mostly] an MS/WinX thing [to the detriment of the rest of the world ;-)]. They may not be supported by all compilers/libraries.
  5. [AFAIK--and I could be wrong about this] MS wide chars have different encodings than utf-8 codepoints. And, they have different multipoint escape codes.

The references that I use:

  1. https://en.wikipedia.org/wiki/UTF-8
  2. https://en.wikipedia.org/wiki/Wide_character

Here is a function that gets the utf-8 code point length within the string:

// 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;
}
  • Related