47 lines
836 B
C++
47 lines
836 B
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 07/03/2025
|
|
|
|
es5A: Scrivere una funzione che ricevuti in input
|
|
una stringa str e un carattere ch, modifichi la
|
|
stringa str duplicando ogni occorrenza del carattere ch.
|
|
Prototipo richiesto:
|
|
char *dupchar(char * const str, char ch);
|
|
Esempio:
|
|
"12 ottobre 1492", 'o' -> "12 oottoobre 1492"
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
#define SIZE 100+1
|
|
|
|
using namespace std;
|
|
|
|
char *dupchar(char * const str, char ch);
|
|
|
|
int main(void) {
|
|
char str[SIZE];
|
|
char ch;
|
|
|
|
cin.getline(str, SIZE);
|
|
|
|
cin >> ch;
|
|
|
|
cout << dupchar(str, ch);
|
|
|
|
return 0;
|
|
}
|
|
|
|
char *dupchar(char * const str, char ch) {
|
|
for (int i = 0; str[i] != '\0'; i++) {
|
|
if (str[i] == ch) {
|
|
memmove(str + i + 1, str + i, strlen(str + i) + 1);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return str;
|
|
} |