Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: no idea

  1. #1
    Join Date
    Jan 2012
    Beans
    160

    no idea

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    
    
      int argument, letter_position, number_of_characters;
      char current_character;
    
    
      for(argument = 1; argument <= argc; argument++){
        number_of_characters = strlen(argv[argument]);
        printf("%d number of characters debug", number_of_characters);
        getchar();
        for(letter_position = 0; argv[argument][letter_position] != '\0'; letter_position++){
          printf("starting loop, position %d", letter_position + 1);
          getchar();
          current_character = argv[argument][letter_position];
          if(current_character == 'A' || 'a'){
    	printf("%d letter of %d argument is A\n", letter_position + 1, argument);
          }
          else if(current_character == 'E' || 'e'){
    	printf("%d letter of %d argument is E\n", letter_position + 1, argument);
          }
          else if(current_character == 'I' || 'i'){
    	printf("%d letter of %d argument is I\n", letter_position + 1, argument);
          }
          else if(current_character == 'O' || 'o'){
    	printf("%d letter of %d argument is O\n", letter_position + 1, argument);
          }
          else if(current_character =='U' || 'u'){
    	printf("%d letter of %d argument is U\n", letter_position + 1, argument);
          }
        }
      }
           
    
    
      return 0;
    }
    Basically im trying to filter through each letter of each argument and spit out the vowels. I even included debug statements, cannot for the life of me understand why this is the output:

    Code:
    ./test13 this is a string
    4 number of characters debug
    starting loop, position 1
    1 letter of 1 argument is A
    starting loop, position 2
    2 letter of 1 argument is A
    starting loop, position 3
    3 letter of 1 argument is A
    starting loop, position 4
    4 letter of 1 argument is A
    2 number of characters debug
    starting loop, position 1
    1 letter of 2 argument is A
    starting loop, position 2
    2 letter of 2 argument is A
    1 number of characters debug
    starting loop, position 1
    1 letter of 3 argument is A
    6 number of characters debug
    starting loop, position 1
    1 letter of 4 argument is A
    starting loop, position 2
    2 letter of 4 argument is A
    starting loop, position 3
    3 letter of 4 argument is A
    starting loop, position 4
    4 letter of 4 argument is A
    starting loop, position 5
    5 letter of 4 argument is A
    starting loop, position 6
    6 letter of 4 argument is A
    zsh: segmentation fault  ./test13 this is a string
    what is going on?

  2. #2
    Join Date
    Apr 2012
    Beans
    5,814

    Re: no idea

    Code:
    current_character == 'A' || 'a'
    ... does not mean what you think it means (HINT: is the value of 'a' true or false?)

  3. #3
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    Here i added more debugging:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    
    
      int argument, letter_position, number_of_characters;
      char current_character;
    
    
      for(argument = 1; argument <= argc; argument++){
        number_of_characters = strlen(argv[argument]);
        printf("%d number of characters debug", number_of_characters);
        getchar();
        for(letter_position = 0; argv[argument][letter_position] != '\0'; letter_position++){
          printf("starting loop, position %d", letter_position + 1);
          getchar();
          current_character = argv[argument][letter_position];
          printf("%c", current_character);
          getchar();
          if(current_character == 'A' || 'a'){
    	printf("%d letter of %d argument is A\n", letter_position + 1, argument);
          }
          else if(current_character == 'E' || 'e'){
    	printf("%d letter of %d argument is E\n", letter_position + 1, argument);
          }
          else if(current_character == 'I' || 'i'){
    	printf("%d letter of %d argument is I\n", letter_position + 1, argument);
          }
          else if(current_character == 'O' || 'o'){
    	printf("%d letter of %d argument is O\n", letter_position + 1, argument);
          }
          else if(current_character =='U' || 'u'){
    	printf("%d letter of %d argument is U\n", letter_position + 1, argument);
          }
        }
      }
           
    
    
      return 0;
    }
    here is the output:

    Code:
    ./test13 this is a string
    4 number of characters debug
    starting loop, position 1
    t
    1 letter of 1 argument is A
    starting loop, position 2
    h
    2 letter of 1 argument is A
    starting loop, position 3
    i
    3 letter of 1 argument is A
    starting loop, position 4
    s
    4 letter of 1 argument is A
    2 number of characters debug
    starting loop, position 1
    i
    1 letter of 2 argument is A
    starting loop, position 2
    s
    2 letter of 2 argument is A
    1 number of characters debug
    starting loop, position 1
    a
    1 letter of 3 argument is A
    6 number of characters debug
    starting loop, position 1
    s
    1 letter of 4 argument is A
    starting loop, position 2
    t
    2 letter of 4 argument is A
    starting loop, position 3
    r
    3 letter of 4 argument is A
    starting loop, position 4
    i
    4 letter of 4 argument is A
    starting loop, position 5
    n
    5 letter of 4 argument is A
    starting loop, position 6
    g
    6 letter of 4 argument is A
    zsh: segmentation fault  ./test13 this is a string
    everything is going as expected, except each current_char is interpreted as an A or a....

  4. #4
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    oh i think i know what you are getting at....

  5. #5
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    Thought i did but i didnt. I tried putting the characters in double quotes. wont compile...
    how come this works?

    Code:
    include <stdio.h>
    
    
    int main(int argc, char *argv[])
    {
      if(argc !=  2){
        printf("ERROR : You need one argument.\n");
        return 1;
      }
    
    
      int i = 0;
      for(i = 0; argv[1][i] != '\0'; i++){
        char letter = argv[1][i];
        
        switch(letter){
        case 'a':
        case 'A':
          printf("%d: 'A'\n", i);
          break;  
    
    
        case 'e':
        case 'E':
          printf("%d: 'E'\n", i);
          break;
    
    
        case 'i':
        case 'I':
          printf("%d: 'I'\n", i);
          break;
    
    
        case 'o':
        case 'O':
          printf("%d: 'O'\n", i);
          break;
    
    
        case 'u':
        case 'U':
          printf("%d: 'U'\n", i);
          break;
    
    
        case 'y':
        case 'Y':
          if(i > 2){
    	printf("%d: 'Y'\n", i);
          }
          break;
          
        default:
          printf("%d: %c is not a vowel\n", i , letter);
        }
      }
      return 0;
    }
    That one does the job but the other one does not.....

  6. #6
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    I GOT IT. needed to do some research. looks like i needed

    if(current_character == 'A' || current_character == 'a')

    going to try this then either mark it as solved or whine some more

  7. #7
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    ok i got it to do what i wanted, however there is still a segmentation fault at the end....any hints?

  8. #8
    Join Date
    Apr 2012
    Beans
    5,814

    Re: no idea

    at a guess, because argv is indexed from 0 (the name by which the program was invoked) to argc-1 - so your outermost loop likely needs to run from 1 to argument<argc (rather than argument<=argc)

  9. #9
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    let me digest that...

  10. #10
    Join Date
    Jan 2012
    Beans
    160

    Re: no idea

    solved. I am going to try to understand what you said steeldriver

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •