Question

Write a program that replace repeated three characters in a string by the character followed by 3...

Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced);

Your program should include the following function:
void replace(char *str, char *replaced);

The function expects str to point to a string, replaced represents the string storing the replaced string. For example, if the str is helllo, the function will store hel3o in replaced.

1) Name your program repeated_chars.c.
2) Assume input is no longer than 1000 characters.
3) The replace function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.
4) To read a line of text, use the read_line function (the pointer version) in the lecture notes.

MY CURRENT ISSUE:

I am having trouble with #3 of the assignment. "The replace function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function." How do I go about using pointer arithmetic to eliminate the loop index variables and all use of the [] operator in the function.

Here's what my code looks like:

#include <stdio.h>

#include <stdlib.h>

#define SIZE 1000

int read_line(char *str, int n);

void replace(char *str, char *replaced);

int main(){

char str[SIZE + 1];

char replaced[SIZE + 1];

printf("Please enter a word: ");

read_line(str, SIZE);

replace(str, replaced);

printf("Output: %s", replaced);

return 0;

}

void replace(char *str, char *replaced){

int i, j = 0;

// loop till end of the first line

for(i = 0; *(str + i) != '\0'; i++){

//check the first, second and third characters in the string

if((*(str + i) == *(str + (i + 1))) && (*(str + i) == *(str + (i + 2)))){

// replace the character

*(replaced + j) = *(str + i);

j++;

// assign 3 to the replaced string

*(replaced + j) = '3';

j++;

i += 2; // increment the index of the original string

}

else{

*(replaced + j) = *(str + i);

j++;

}

}

// terminates string

*(replaced + j) = '\0';

}

int read_line(char *str, int n){

int ch;

int i = 0;

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

if(i < n){

*str++ = ch;

i++;

}

}

*str = '\0'; // terminates string

return i; // number of characters stored

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

void replace(char *str, char *replaced) {
        int count = 0;
        int lastChar = '\0';

        while(*str != '\0') {
                if(lastChar == *str) {
                        count++;
                } else {
                        if(count == 1) {
                            *replaced = lastChar;
                            replaced++;
                        } else if(count == 2) {
                            *replaced = lastChar;
                            replaced++;
                            *replaced = lastChar;
                            replaced++;
                        } else {
                                *replaced = lastChar;
                                replaced++;
                                *replaced = '3';
                                replaced++;
                        }

                        lastChar = *str;
                        count = 1;
                }
                str++;
        }

        if(count != 0) {
                if(count == 1) {
                    *replaced = lastChar;
                    replaced++;
                } else if(count == 2) {
                    *replaced = lastChar;
                    replaced++;
                    *replaced = lastChar;
                    replaced++;
                } else {
                        *replaced = lastChar;
                        replaced++;
                        *replaced = '3';
                        replaced++;
                }
        }
        *replaced = '\0';
}

int read_line(char *str, int n) {

        int ch;
        int i = 0;

        while((ch = getchar()) != '\n'){
                if(i < n){
                        *str++ = ch;
                        i++;
                }
        }
        *str = '\0'; // terminates string
        return i; // number of characters stored
}

int main(void) {
        char str[1000];
        char replaced[1000];
        printf("Enter input: ");
        read_line(str, 1000);

        replace(str, replaced);

        printf("\nEncoded:\n%s", replaced);

        return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a program that replace repeated three characters in a string by the character followed by 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a program that can remove spaces from an input string, find the indexes of a...

    Write a program that can remove spaces from an input string, find the indexes of a character within the string and replace that character with another character. Here is an example input: I am an input string a b The first line, "I am an input string" represents the input string. Please put it into a string variable using getline. In the second line "a b", a is the character that needs to be located within the input string, and...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • 1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters...

    1 (continued) b. (20 points) Complete the function with the prototype and description below. void removeNonLetters (char estr); This function should remove all characters in string str except letters. Remember that, since the string is passed by address, changes made to str inside the function are seen outside the function. For example, if the string 1. ERCE.2160 Fall 2019" before calling this function, then after calling removeNonLetters (1). 51 - "EECEFall". Hint: to access part of a string (a substring)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT