Thread: How do I replace two a's in a string with an '*' unknown input length

  1. #1
    Registered User
    Join Date
    Apr 2018

    How do I replace two a's in a string with an '*' unknown input length

    Hey guys so for my assignment I have to replace two a's with an '*' from a string of unknown input length in C. The challenge is I am only allowed to use the getchar() and the putchar() function.
    With an array it would have been easier but I am not even allowed to use that.

    This is what I have tried

    But it replaces all the a's with '*'.

    #include <stdio.h>
    int ch;
    int main()
        while ((ch = getchar()) !=EOF && ch != '\n'  ) { 
        return 0;
    Could someone just guide me to the correct path or just tell me which approach I should take?

    Thank you in advance.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    You're almost there. You just have to call getchar again at the start of the first if block.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input string of unknown length in c
    By Obvious Guy in forum C Programming
    Replies: 4
    Last Post: 04-13-2018, 02:46 PM
  2. Reading in file of unknown length
    By zone159 in forum C Programming
    Replies: 2
    Last Post: 11-14-2012, 02:07 PM
  3. Reading from a textfile with unknown length?
    By chickenlittle in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2011, 11:59 PM
  4. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  5. Replies: 1
    Last Post: 07-10-2006, 06:30 AM

Tags for this Thread