lundi 2 avril 2018

Replacing three 'a' in with a single '*' in a string

So my program should get input from an user and store it in an array. After that if the input string includes three 'a's in a row it should be replaced with a single '*'. However I can't seem to get it right. It only replaces the first a with a *. I tried to replace the following 2 a with a blank but the output looks funny. For this exercise I have to use putchar() and getchar().

Thank you in advance.

#include <stdio.h>

char c;
char buffer[256];

int counter= 0;

int i;

int main()

{



while ((c = getchar()) != '\n'){

buffer[counter] =c;
counter++;

if (counter >255) {
break;
}




}

for(i=0; i<256;i++){

if(buffer[i]== 'a'&&buffer[i+1]=='a'&&buffer[i+2]=='a')
{

buffer[i]= '*';
buffer[i+1]=' ';
buffer[i+2]=' ';

}




putchar(buffer[i]);

}

putchar('\n');



return 0;

}

Aucun commentaire:

Enregistrer un commentaire