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

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    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.


    Code:
       #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;
    
        }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Unless you want to do a lot of swapping around, you need two arrays for this. For example:
    Code:
    for (i = 0, j = 0; i < 256; ++i, ++j) {
        if (buffer[i] == 'a' && buffer[i+1] == 'a' && buffer[i+2] == 'a') {
            buffer2[j] = '*';
            i += 2;
        } else {
            buffer2[j] = buffer[i];
        }
    
        putchar(buffer2[j]);
    }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    thank you for your answer.
    But for this exercise I am only allowed to use one array with a size of 256.
    could you tell me what exactly you mean with swapping around?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I meant that you have to move the rest of the array elements two places back each time you find a match. Like this:
    Code:
    if(buffer[i] == 'a' && buffer[i+1] == 'a' && buffer[i+2] == 'a') {
        buffer[i]= '*'; 
        for (j = i+3; j < 256; ++j) {
            buffer[j-2] = buffer[j];
        }
    }
    Last edited by GReaper; 04-02-2018 at 05:46 AM.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You don't need a second array and you don't need to move things around all the time either.
    Code:
    #include <stdio.h>
    
    int main() {
        char b[256], c;
    
        int size = 0;
        while ((c = getchar()) != '\n') {
            if (size == 256) break;
            b[size++] = c;
        }
    
        int from = 0, to = 0;
        while (from < size) {
            if (from < size - 2 && b[from]   == 'a'
                                && b[from+1] == 'a'
                                && b[from+2] == 'a') {
                b[to++] = '*';
                from += 3;
            }
            else
                b[to++] = b[from++];
        }
        size = to;
    
        for (int i = 0; i < size; i++)
            putchar(b[i]);
        putchar('\n');
    
        return 0;
    }
    Or you could do it as you read in the line.
    Code:
    #include <stdio.h>
    
    int main() {
        char b[256], c;
    
        int size = 0, acnt = 0;
        while ((c = getchar()) != '\n') {
            if (size == 256) break;
            if (c == 'a') {
                if (++acnt == 3) {
                    b[size++] = '*';
                    acnt = 0;
                }
            }
            else {
                for ( ; acnt > 0; --acnt) b[size++] = 'a';
                b[size++] = c;
            }
        }
        for ( ; acnt > 0; --acnt) b[size++] = 'a';
    
        for (int i = 0; i < size; i++)
            putchar(b[i]);
        putchar('\n');
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2017, 09:41 AM
  2. Replacing part of a string?
    By potatoes1 in forum C Programming
    Replies: 1
    Last Post: 05-25-2017, 06:25 PM
  3. Replies: 7
    Last Post: 03-17-2012, 09:36 PM
  4. replacing a charecter in a string
    By Dontgiveup in forum C Programming
    Replies: 10
    Last Post: 06-03-2009, 11:54 PM
  5. replacing string in file except the first
    By jetfreggel in forum C Programming
    Replies: 4
    Last Post: 01-12-2003, 03:03 PM

Tags for this Thread