Thread: C program arrays - strings

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    C program arrays - strings

    Hi everyone!

    I started a month ago learning the language of C. Its very difficult sometimes for me to figure out the code, and even it is difficult to think how to find the algorithm.
    I would like to ask a question about a programming task:
    "Write a program that receives a seven-digit phone number and find out which words can be formed by this number that included in the file words.txt."
    So i write a seven-digit number and the program checks which words fits from txt file words.txt.
    The ratio of numbers - letters is like this:
    Imageshack - thld.jpg
    I thought that i can have two arrays, one to check the numbers and one to check the letters, but i dont know how to build them.
    (Sorry for my bad english)

    I would be grateful for some help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This solution will not find words which are made of multiple letters, from the same number. If "ABA" was a word, then this algorithm wouldn't find it.

    1) Put all the seven letter long words, into a separate array of strings or file. strlen(word) could help.
    2) Use nested for loops (7 of them), to represent each wheel of a 7 wheel odometer. Set each wheel to it's lowest possible value, and it's upper limit in the for loop, as it's highest possible char, according to your picture.

    3) Now have the wheels find and output all permutations of the letters for you. The words from #1 should be sorted, for fast searching.

    4) Use a binary search on the output of all the words you get from the "wheels". Use if((strcmp(str1, str2))==0) to find out if any permutations match a word from the 7 letter word list.
    start of the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
      char w1, w2, w3, w4, w5, w6, w7;
      for(w1='a';w1<'c';w1++) {
         for(w2='c';w2<'e';w2++) {
             (repeat for all 7 "wheels", comparing"words"
              found, with actual words from the 7 letter
              word files (or array).
                
        }
      }
    
      return 0;
    }
    Sound like a plan?
    Last edited by Adak; 10-07-2010 at 05:39 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Hello again!

    I realy don't understand what do you mean!!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Simple solution with NO C code given.

    Part 1. Read in words.txt and create numbers.txt

    numbers.txt is the words changed to numbers. This can be just an array in memory; but not sure about you C programming knowledge; this can be hard if the numbers of words is not known or at least the max number needed to be supported is not know.

    Part 2. Take the "input number" and search it for values in the numbers.txt file.

    You could use the C function "strstr" to do the searching.

    Note, once you get the two parts working; you should be able to merge them into a single program. I just separated the two to break down the task.

    Note: I am not certain what problem Adak was solving; I am sick right now, might be something I will understand later.

    Tim S.
    Last edited by stahta01; 10-11-2010 at 04:29 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Hello again!
    Thank you for your answers, my program works now
    Cheers!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  5. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM