I am working on a program that ask the following:
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:
Code:
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.
I have the program working correctly but it doesn't follow number 3. Here's what my code looks like:
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000 // forgive me for I have sinned
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
}
How do I go about eliminating the loop index to get the same output that I'm looking for? I'm assuming I have to use a pointer in my for loop, but I'm not entirely sure if that's correct. I'm hoping someone can give me some references to point me in the right direction.
Also, here is the shell script we're using to test our inputs
Code:
# try_repeated is a Unix shell script that will be used to test project 5.
# To use the script, copy it into the same directory as your scource file
# Set execute permission for the file by issuing the command:
# chmod +x try_repeated
# Compile your program, producing a.out as the executable
# To run the script, type
# ./try_repeated
# The user input from the script will not be shown on the screen.
# Compare the results from your program with the expected results on the test cases.
echo '===================================================='
#
./a.out <<-EndOfInput
topssecret
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Enter a word: topssecret'
echo 'Output: topssecret'
#
echo '===================================================='
#
./a.out <<-EndOfInput
fleeenow
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Enter a word: fleeenow'
echo 'Output: fle3now'
#
echo '===================================================='
#
./a.out <<-EndOfInput
asssasssinate
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Enter a word: asssasssinate'
echo 'Output: as3as3inate'
echo '===================================================='
#
./a.out <<-EndOfInput
defendddd
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Enter a word: defendddd'
echo 'Output: defend3d'
echo '===================================================='
Sorry for the long post... Thank you in advance!