Thread: search char array for specific characters and replace with another

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    search char array for specific characters and replace with another

    Code:
    #include <stdio.h>
    #include <string.h>
    Code:
    #define MAXSIZE 255
    void arrayRev(char arrayDna[]);
    int main(void){
       char arrayDna[MAXSIZE];
       while((arrayDna[0] != 'e') && (arrayDna[1] != 'x') && (arrayDna[2] != 'i') && (arrayDna[3] != 't')){
          printf("Enter a DNA sequence consisting of A's, T's, G's, and C's or 'exit' to exit.\n");
          scanf("%c", arrayDna);
       }
       arrayRev( arrayDna );
       return 0;
    }
    void arrayRev(char arrayDna[]){
       if(arrayDna[MAXSIZE] == 'a'){
          printf('t');
       }
       if(arrayDna[MAXSIZE] == 't'){
          printf('a');
       }
       if(arrayDna[MAXSIZE] == 'c'){
          printf('g');
       }
       if(arrayDna[MAXSIZE] == 'g'){
          printf('c');
       }
    }
    
    





    If anyone could tell me how to post the actual program instead of just the text that would help, otherwise im just trying to replace any "a" in the entered sequence with a "t", any "t"s with "a"s, any "g"s with "c"s, and "c"s with "g"s and then reprint it out. im guessing i have to do something with changing the address but Im stuck.

  2. #2
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    I actually didn't understand very well what you're trying to implement.

    Could you be more accurate on describing what you're trying to do and what is your difficulty?

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Quote Originally Posted by DeliriumCordia View Post
    I actually didn't understand very well what you're trying to implement.

    Could you be more accurate on describing what you're trying to do and what is your difficulty?
    Code:
    int main(void){
       char arrayDna[MAXSIZE];
       while(arrayDna[0] != 'e' && arrayDna[1] != 'x' && arrayDna[2] != 'i' && arrayDna[3] != 't'){
          printf("Enter a DNA sequence consisting of A's, T's, G's, and C's or 'exit' to exit:\n");
          scanf("%s", arrayDna);
          printf("The compliment DNA sequence is:");
          arrayRev( arrayDna );
          printf("\n");
          arrayCount( arrayDna );
          arrayStart( arrayDna );
          arrayStop( arrayDna );
       }
       return 0;
    }
    i actually figured out my original question. I'm having trouble with the while loop now. Any time the user enters an "e" as the first character, or "x" as second character, or "i" as third, or "t" as fourth, it exits the loop. But I want it to exit when "exit" is entered, not when just one of those letters is in that position. I know i have an "and" operator not "or" so I dont know my problem. Thank you.

  4. #4
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    The problem is you should iterate with a counter i (if I correctly understood what you want to do).
    If I understood what you want to do, you should write some char entries, and when you want to finish you write exit to stop the execution, right?
    Like that, it stops only when the first character of the array is e, the second is x etc. It doesn't make a lot of sense. For example the entry x e x i t will not terminate your program, because arrayDna[0] is x, and you will never get out of while loop.

    It should be better to use parameters like arrayDna[i]!='e'&& arrayDna[i+i]!='x' etc, so you can check this condition each time you increment your i.

    Another thing.

    Usually the algorithm for filling arrays with chars, consists in scanning the line, and when you hit Enter you finish the line.

    In the while loop, you check if next char is EOL (end of line, it means you have no more chars to insert in the array). If it's not EOL, you insert the next char in the array, and so on.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try something like this:
    Code:
    int main(void){
       char arrayDna[MAXSIZE];
       while(1) {
          printf("Enter a DNA sequence consisting of A's, T's, G's, and C's or 'exit' to exit:\n");
          scanf("%s", arrayDna);
          if (strcmp(arrayDna, "exit") == 0)
             break;
          printf("The compliment DNA sequence is:");
          arrayRev( arrayDna );
          printf("\n");
          arrayCount( arrayDna );
          arrayStart( arrayDna );
          arrayStop( arrayDna );
       }
       return 0;
    }
    Also, a DNA complement is spelled with an 'e', not an 'i'.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    yes i just need it to end when "exit" is entered. I have something seperate for invalid sequences. i just dont understand why my "&&" is working more as an OR operator than an AND operator. only one of the constraints needs to be met instead of all 4 for the while loop to end.

    Also, when I do enter "exit" it still prints out all the stuff within the while loop and then exits.

    I will work on the EOL, thank you.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by chelpplease View Post
    Code:
    int main(void){
       char arrayDna[MAXSIZE];
       while(arrayDna[0] != 'e' && arrayDna[1] != 'x' && arrayDna[2] != 'i' && arrayDna[3] != 't'){
          printf("Enter a DNA sequence consisting of A's, T's, G's, and C's or 'exit' to exit:\n");
          scanf("%s", arrayDna);
          printf("The compliment DNA sequence is:");
          arrayRev( arrayDna );
          printf("\n");
          arrayCount( arrayDna );
          arrayStart( arrayDna );
          arrayStop( arrayDna );
       }
       return 0;
    }
    i actually figured out my original question. I'm having trouble with the while loop now. Any time the user enters an "e" as the first character, or "x" as second character, or "i" as third, or "t" as fourth, it exits the loop. But I want it to exit when "exit" is entered, not when just one of those letters is in that position. I know i have an "and" operator not "or" so I dont know my problem. Thank you.
    (Emphasis mine)
    You have and operators as you correctly suspect you need, but you're using "not equal" instead of "equal". Notice you state "'e' as the first character". That implies you want the first character (arrayDna[0] to equal 'e'). Better yet, use strcmp to check for "exit". strcmp returns 0 if the strings match, non-zero if they don't:
    Code:
    while (strcmp(arrayDna, "exit") != 0)  // while the user didn't type "exit"
    But there's a potential bug there. When your program starts, arrayDna is uninitialized, so it could contain anything, including (though unlikely) the string "exit", causing your program to terminate early. I find do-while loops best for user input because you always want to ask for input at least once before deciding whether you should do something or repeat your loop:
    Code:
    do {
        print "Enter a DNA..."
        read user input into arrayDna
        if (strcmp(arrayDna, "exit") != 0) {
            // only find the complement sequence if they didn't type exit
        }
    } while (strcmp(arrayDna, "exit") != 0);  // stop the loop if they type "exit"
    EDIT: As I mentioned, you should have been using arrayDna[0] == 'e' instead of !=. Look up DeMorgan's law (http://en.wikipedia.org/wiki/De_Morgan's_laws) if you want to understand why it looks like OR operators. Basically (a != b && c != d) is equivalent to !(a == b || c == d).
    Last edited by anduril462; 04-02-2012 at 05:07 PM.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Thank you, anduril.

    The strcmp but the only problem I still have is it is still printing everything inside the while loop before it exits.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please post your updated code.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chelpplease View Post
    Thank you, anduril.

    The strcmp but the only problem I still have is it is still printing everything inside the while loop before it exits.
    I'm not clear on the problem. You have print statements inside the loop, and now you don't want them to print??

    Show what you want, in a short example (no code, just input output please), from inside the while loop.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    Adak, if "exit" is entered I only want it to end, without printing everything in the while loop.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I suspect you left out the "if" statement in my example of the do-while loop. If that doesn't solve it, please post your updated code.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by chelpplease View Post
    if "exit" is entered I only want it to end, without printing everything in the while loop.
    Then do it the way I showed in post#5, which is the cleaner way.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post #5, lines 6 and 7. What's wrong with using them? Put them right after the user enters the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. finding length of characters in an assigned char array ??
    By charlatanksk in forum C Programming
    Replies: 6
    Last Post: 05-04-2008, 02:34 PM
  3. Replace AlphaNumeric Characters
    By Hexxx in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2006, 06:12 PM
  4. Search and Replace
    By sh0rty in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2002, 03:26 PM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread