If statement conditions

This is a discussion on If statement conditions within the C Programming forums, part of the General Programming Boards category; Can I use a FOR loop as part of the condition for an IF statement? For example, my problem is ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    it is hard for me to follow your explanations
    could you post the code that shows what are you trying to do but failed?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 07: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, 06: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, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21