Thread: Multidimensional string arrays and comparing characters

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    37

    Multidimensional string arrays and comparing characters

    I'm trying to read in a file and find words that ONLY contain certain characters. For example, I want to find words that only have "ats" so "bats" or "cats" would be invalid but "ats", "stats", "sat" would be valid.

    So far I've gotten my program to count how many times the characters m e l o w show up, which for my test is 37. For my test text file I have
    hello
    low
    meow
    mole
    owl
    dog
    paper
    cat
    books
    marshmellow
    pillow
    moles
    Code:
    int main(void) {
    
       char TEST[5] = {'m', 'e', 'l', 'o', 'w'};
    
       int max_words=12;
       int max_len=12;
       int i,k,j,letter=0, t=0;
       char words[max_words][max_len];
    
       FILE *in = NULL;   
       in = fopen("dummy.txt", "r"); //opens file for reading
    
       //verify that file is open
       if (in == NULL) 
          printf("Unable to open input file.");
    
       //read data
       for(i=0; !feof(in) && i<max_words ;i++) {
          fscanf(in, "%s", &words[i][max_len]);
          printf("%s\n", &words[i][max_len]);
    
       }
       
       //print out characters
       printf("\n");
       for(j=1; j<=max_words; j++) {
          for(k=0;k<max_len;k++) {
             if(words[j][k]!='\0'){
                printf("%c.", words[j][k]);
             }
             else
                break;
             for(t=0;t<5;t++) { //count the number of times the character shows up
             if(words[j][k]==TEST[t])
                letter++;
             }
          }
       printf("\n");
       }
    
       printf("\n=i%d. k=%d letter=%d", i, k, letter);
    
       return 0;  
    }
    Last edited by ArcadeEdge; 02-15-2012 at 12:12 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    not sure what your question is, but one thing i see in your first loop reading in the data,

    &words[i][max_len]

    this is going to offset your data by i+1 in the array. your words[0] element will be empty. i think you want to use '&words][i][0]' or just 'words[i]' when reading the data in. that's why you had to use j=1;j<= max_words in your print loop.

    if your question is how to actually know if the word is valid, it seems that if you set letter = 0 before your loop over the characters in a word ('for(k=...') then after that loop check if 'letter == strlen(word[j])' that would tell you if there were no invalid characters in that word.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    Quote Originally Posted by dmh2000 View Post
    not sure what your question is, but one thing i see in your first loop reading in the data,

    &words[i][max_len]

    this is going to offset your data by i+1 in the array. your words[0] element will be empty. i think you want to use '&words][i][0]' or just 'words[i]' when reading the data in. that's why you had to use j=1;j<= max_words in your print loop.

    if your question is how to actually know if the word is valid, it seems that if you set letter = 0 before your loop over the characters in a word ('for(k=...') then after that loop check if 'letter == strlen(word[j])' that would tell you if there were no invalid characters in that word.
    Thank you! Your solution works perfectly!
    I was over thinking it, thinking I had to compare each character and if doesn't match, throw away the word.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    I have another question.

    What if I wanted to check for capital letters too?
    Can I subtract 32 (dec) from each element in TEST? So I will get the capital letter ascii value?

    for example:
    Code:
    for(t=0;t<5;t++) {
       if(words[j][k]==TEST[t] || words[j][k]==TEST[t]-32)
       ..code...
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ArcadeEdge View Post
    What if I wanted to check for capital letters too?
    Can I subtract 32 (dec) from each element in TEST? So I will get the capital letter ascii value?
    You could, but that's the worst way to do it. First off, 32 is a "magic number". It's cryptic and nobody looking at your code would know what you're doing there, without considerable research. You would be better off using test[t] - ('a' - 'A'), but that's still far from ideal. Subtracting 32 is specific to ASCII, so your application wouldn't work for systems that use, say, EBCDIC. The best solution would be to use the toupper/tolower functions provided by ctype.h:
    Code:
    if (toupper(words[j][k]) == toupper(TEST[t]))
        ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing string arrays
    By never_lose in forum C Programming
    Replies: 17
    Last Post: 03-20-2011, 01:38 AM
  2. Comparing string for blank characters - C programming
    By dannydb368 in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 10:14 PM
  3. Comparing characters
    By Banana Man in forum C++ Programming
    Replies: 28
    Last Post: 01-13-2008, 11:11 PM
  4. Comparing characters
    By Thuz in forum C Programming
    Replies: 2
    Last Post: 09-16-2007, 12:07 PM
  5. Comparing characters.
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2001, 09:50 AM