Thread: If statement conditions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    If statement conditions

    Can I use a FOR loop as part of the condition for an IF statement?

    For example, my problem is to create a program that will read in a string and compare it to a dictionary file in order to find anagrams of the original word. So I've attacked this by comparing the words of the same length, and anywhere that a letter is the same I'm aiming to change that to a character that isn't a letter, so I can use an IF statement in order to see if the temporary string is made up entirely of these characters, in which case it would be an anagram of the word.

    However, because I don't initially know how long the word will be, I've said something like word[i] was the character of the string (where i starts at 0 and increases by 1 everytime), and so wanted to do a for loop until it ran out. Is this allowed by C?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it is hard for me to follow your explanations
    could you post the code that shows what are you trying to do but failed?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Code:
    if(for(k=0;k<strlen(word);k++))
                                          {
                                               temp[k]=@
                                          }, printf("%s is an anagram of %s\n" dict[i].name, word);
    Its a little messy, but thats what I'm trying to do.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use combination of if, for and flag
    Code:
    int match = 1;
    for(k=0;k<strlen(word) && match;k++)
    {
       if(!condition_for_current_k)
         match = 0;
    }
    
    if(match)
       printf("%s is an anagram of %s\n" dict[i].name, word);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Could you run me through in words what this is doing? I don't quite follow it in code.

    Thanks

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you set some flag at the begginign of your check
    then you walk through your string and check characters with your dictionary word
    if characters don't match - you set flag to false and exit the loop

    after the loop is ended you check the value of the flag
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  2. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM