Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "mystring.h"
- char* myreplaceall(char* str, char* suf1, char* suf2);
- char* input_str();
- int main() {
- printf("Input orig string:\n");
- char* message = (char*) input_str();
- printf("Input suffix to change:\n");
- char* suf1 = (char*)input_str();
- printf("Input suffix to change for:\n");
- char* suf2 = (char*)input_str();
- message = myreplaceall(message, suf1, suf2);
- printf("\nResult:\n");
- printf(message);
- }
- char* input_str() {
- int c;
- char* string = malloc(sizeof(char));
- string[0] = '\0';
- int i = 0;
- while ( (c = getchar()) != '\n')
- {
- string = realloc(string, (i + 2) * sizeof(char)); //reallocating memory
- string[i] = (char)c; //type casting `int` to `char`
- string[i + 1] = '\0'; //inserting null character at the end
- i++;
- }
- return string;
- }
- char* myreplaceall(char* str, char* suf1, char* suf2) {
- char* res = (char*)malloc(sizeof(char*));
- res[0] = '\0';
- int i = 0; int i2 = 0; int j = 0;
- while (i < strlen(str)) {
- while ((str[i + j] == suf1[0 + j]) & (j != strlen(suf1))) j++;
- if (j == strlen(suf1)) {
- stradd(&res, suf2);
- i = i + strlen(suf1);
- i2 = i2 + strlen(suf2);
- }
- else {
- char* c = malloc(sizeof(char) * 2);
- c[1] = '\0';
- c[0] = str[i];
- stradd(&res, c);
- i++; i2++;
- }
- j = 0;
- }
- res[i2] = '\0';
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement