Thread: simple question URGENT

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    simple question URGENT

    hi all
    say if i were to use getchar(); and i was to the character as many times as it's entered (since it's goign to be a in a loop) yet for the last repetition, it is a different character, for example;
    if i entered rrrr and i wanted the last character to be changed to t. the end character is defined to be preceeding a end of character ascii digit (10).
    it would produce rrrt
    thanks in advance.
    Last edited by captain_jack; 03-23-2006 at 08:29 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I have no idea what you just said. Can you restate the question more clearly?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    lets say my input was of unknown length of a single character repeated a number of times (through getchar())
    such as rrrrrr or whatever
    and i had to convert it to repeat what i said (echo) except for the last character which had to be converted to a particular letter. the question that im doing is asking me to convert the last letter of the input characters into t
    so if i run the program and type in "rrrr"
    it would echo all of it except for the last letter which would write a z instead, so it would produce "rrrt"
    with the same program, if i inputted "rrrrrrrrrr" it would print "rrrrrrrrrt" (jus the last character is converted into a different predefined chracter. note that an input of 'r' would give the output 't'.
    this is done using while loop with getchar and if/else statements
    but i dont know how to do it
    thanks!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    So do you want to echo it as you type, or once return is pressed? Does is need to be a single repeated char? eg if you entered rrrrrrrrrssssssssss what would you expect? rrrrrrrrtssssssssst ?

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Count the characters entered. Output the character count-1 times. Then output the new character.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Quote Originally Posted by WaltP
    Count the characters entered. Output the character count-1 times. Then output the new character.
    sorry i dont know how to count the times itwas entered??

    also
    how would i get a new line in a situation like this

    Code:
    #include <stdio.h>
    
    int
    main (int argc, char **argv)
    {
        
    char ch;
     
     
      
        while ((ch=getchar()) !=EOF){
           
            
        
          if(ch =='r'){
             printf("t");
          }
          
        return 0;
        
    }

    HELPP it prints t, but then when i start to type more text into the program (line by line) it isn't on a new line!!!
    i tried putting printf("\n"); in multiple spots but its not working
    thanks!!!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    yeah sorry it is
    where im from i posted that in the middle of the night
    thanks for the replies

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by captain_jack
    sorry i dont know how to count the times itwas entered??
    Really? Did you think about it for a moment? Using your code:
    Code:
    while ((ch = getchar()) != EOF)
    {
        count++;
        if(ch =='r')
        {
            printf("t");    // putchar('t'); would be a better choice here.
        }
        return 0;
    }
    Oh, and work on your formatting. Whitespace is your friend on a line, but not as much between lines. And consistant indenting really helps readability -- as I formatted above...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Replies: 5
    Last Post: 10-08-2004, 05:30 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM