Add character
Mar 25, 2021 at 9:59am UTC
I want to make a function “addChars” that displays after each vowel "*"
It must change the variable on call.
EX: char s[200];
s[0] = 'a';
s[1] = 'n';
s[2] = 'a';
s[3] = 0;
addChars(s);
cout << s; => a*na*
In my code:
addChars(s) => a*na*
cout << s; => ana
=> a$na$ana
It only needs to display the modified s variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <cstring>
using namespace std;
void addChars(char sir[201]) {
int i;
int n = strlen(sir);
if (strchr(sir, 'a' ) || strchr(sir, 'e' ) || strchr(sir, 'i' ) || strchr(sir, 'o' ) || strchr(sir, 'u' )) {
for (i = 0; i < n; i++) {
cout << sir[i];
if (strchr("aeiou" , sir[i]) != 0) {
cout << "*" ;
}
}
}
}
int main() {
char s[200];
s[0] = 'a' ;
s[1] = 'n' ;
s[2] = 'a' ;
s[3] = 0;
addChars(s);
cout << s;
}
Mar 25, 2021 at 10:10am UTC
Try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
constexpr size_t MAXSTR {200};
using Str = char [MAXSTR];
void addChars(Str sir) {
Str temp {};
for (char * str = sir, *t = temp; *str; ++str, ++t)
if (strchr("aeiou" , *t = *str) != nullptr )
*++t = '*' ;
strcpy(sir, temp);
}
int main() {
Str s {"ana" };
addChars(s);
cout << s << '\n' ;
}
Last edited on Mar 25, 2021 at 10:13am UTC
Topic archived. No new replies allowed.