mardi 16 novembre 2021

Replacing a pair of charekters in a string with another

I want to replace a pair of charaters with another pair.

For example if want to replace "ax" with "57" and the string is "vksax", it will give out "vks57".

Tried it this way but get a weird output that is only partially correct:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   
char dummy;
int i;
char string[16];
scanf("%s", string);

for(i=0;string[i];i++){
  if(string[i] == 'a'  && string[i+1] == 'x'){
    string[i] = '5' ;
    string[i+1] = '7';
    i=0;  
  }
printf("%s", string) ;
}
   exit(0);
}

I cant figure out why it gives out , for example with an input of 1234ax the output 1234ax1234ax1234ax1234ax123457123457123457123457123457123457

Aucun commentaire:

Enregistrer un commentaire