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. I am just allowed to use loops and ifs.
This is what I have tried. But it replaces all the a's with '*'.
This is what I have so far.
Is this even the right approach?
It just replaces all the a's with a '*'. But it should only replace two a's with a '*'.
Could someone help me or at least lead me to the right solution?
Thank you in advanc.
With an array it would have been easier but I am not even allowed to use that. I am just allowed to use loops and ifs.
This is what I have tried. But it replaces all the a's with '*'.
This is what I have so far.
Code:
#include <stdio.h>
int ch;
int main()
{
while ((ch = getchar()) !=EOF && ch != '\n' ) {
if(ch=='a'){
if(ch=='a')
{
ch='*';
}
}
putchar(ch);
}
putchar('\n');
return 0;
}
Is this even the right approach?
It just replaces all the a's with a '*'. But it should only replace two a's with a '*'.
Could someone help me or at least lead me to the right solution?
Thank you in advanc.