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
}
#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; }
Write a program that replace repeated three characters in a string by the character followed by 3...
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...
Using C(89). A fashion retailer has decided to provide discount codes to teachers and students during a promotional event. The first step is to verify the teacher/student status of people requesting discount code by their email addresses. They will check if the email addresses end with ".edu" and find the domain name if it does before further verification. Write a program to find the domain name of an email address if the domain name ends with .edu. The program displays...
#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 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 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 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 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);...
Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...
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 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....