-1

The goal is to insert "E" after all occurrences of the letter "T"

But this is what the code below does:

Soon as the character "T" is detected...

It replaces "T" with another "T" then inserts "E"

How can it be altered so it does not replace "T" with another "T" because it seems like extra work.

Instead it can simply leave alone the existing "T" in place.. move AFTER it and insert the "E".

char s1[1024];
int i, n;

  for (i=0, n = 0; s[i]!= '\0'; i++)
  {
    if (s[i] == 'T')
    {
            s1[n] = 'T';
            n++;
            s1[n] = 'E';
            n++;
    }
    else
    {
        s1[n] = s[i];
        n++;
    }
}
s1[n] = '\0';
Shallon
  • 35
  • 3
  • `s1` and `s` are different. Does what you intend mean to insert in `s`? – BLUEPIXY Jul 28 '17 at 09:59
  • `s1` is the new modified version of `s`. – Shallon Jul 28 '17 at 10:00
  • In that case T is not replaced by T but simply means copy. – BLUEPIXY Jul 28 '17 at 10:01
  • @BLUEPIXY, well everything else is copied regularly. why does T have to be copied in a special way ?. I suppose I asked the question wrong. but how do I ensure T is copied regularly ? – Shallon Jul 28 '17 at 10:03
  • E.g `s1[n++] = s[i]; if(s[i] == 'T') s1[n++] = 'E';` or `if((s1[n++] = s[i]) == 'T') s1[n++] = 'E';` – BLUEPIXY Jul 28 '17 at 10:05
  • 2
    Isn't this very much the same question as [your previous one](https://stackoverflow.com/q/45367910/2371524)? Please don't "spam" questions but take some time before to clarify yourself what exactly you want to ask. –  Jul 28 '17 at 10:22
  • 1
    Didn't you just ask a nearly identical question about "%" -> "%%" a few hours ago? – Gerhardh Jul 28 '17 at 10:31

1 Answers1

0

Just copy characters and when you see you have copied T, copy an E

s1[n] = s[i];
n++;    
if (s[i] == 'T')
{
  s1[n] = 'E';
  n++;
}
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34