Reputation: 707
I want to get all vowel substrings from the given string. Given string is 'auiouxaeibaou', get substrings from the given string like [auiou,aei,aou].
Here I tried something like this, but not getting the exact output.
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s \n\n ", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
char str[] = "aeixae";
int length = strlen(str);
int start_index = 0, end_index = 0;
for (int x=0; x<length; x++) {
char c = str[x];
if (isVowel(c) == false) {
end_index = x;
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
}
}
return 0;
}
Upvotes: 2
Views: 4145
Reputation: 136
For fun, here is a simple way to do it without substrings, just print vowels as they come and add newlines the first time you hit a consonant.
#include <stdio.h>
#include <string.h>
int isVowel(char c){
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
return 1;
}
else{
return -1;
}
}
int main()
{
char my_str[] = "auiouxxxxxxaeibaou";
int todo_newline = 1; // prevent consecutive newlines
for(int i = 0; my_str[i] != '\0'; i++){
if(isVowel(my_str[i]) == 1){
printf("%c", my_str[i]);
todo_newline = 1;
}
else{
if(todo_newline == 1){ // print one newline
printf("%c", '\n');
todo_newline = -1; // don't print consecutive newlines
}
}
}
}
Upvotes: 1
Reputation: 154305
Alternative approach:
strspn()
, strcspn()
are the best tools here. @Gem Taylor
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "aeixae";
const char *s = str;
while (*(s += strcspn(s, "aeiou")) != '\0') { // skip letters that are not aeiou
size_t len = strspn(s, "aeiou"); // find length made up of aeiou
printf("<%.*s>\n", (int) len, s); // print sub string
s += len;
}
}
Output
<aei>
<ae>
Upvotes: 0
Reputation: 3671
your attempt was close. I just added the includes and made sure that the last part of the string also was printed. See the termination of the for loop and the if where you check for the vowel.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s \n\n", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
char str[] = "aeixae";
int length = strlen(str);
int start_index = 0, end_index = 0;
for (int x=0; x<=length; x++) {
if (x == length || !isVowel(str[x])) {
end_index = x;
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
}
}
return 0;
}
and this is the output:
gcc main.c && ./a.out
aei
ae
Upvotes: 2
Reputation: 32596
but not getting the exact output.
why you don't have the expected result :
for (int x=0; x<length; x++)
by for (int x=0; x<=length; x++)
because the null character is not a vowel (that does not produce illegal access)The modifications gives that (I changed the input string) :
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s \n\n ", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
char str[] = "aeixaewwii";
int length = strlen(str);
int start_index = 0, end_index = 0;
bool wasVowel = false;
for (int x=0; x<=length; x++) {
char c = str[x];
if (isVowel(c) == false){
end_index = x;
if (wasVowel)
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
wasVowel = false;
}
else
wasVowel = true;
}
return 0;
}
BTW : 'y' is a vowel for me, you missed it in isVowel()
Upvotes: 2
Reputation: 141483
size_t
as the proper type returned by strlen()
.auiouxaeibaou
, you need to insert it at char str[] = "aeixae";
substr
function!x
reaches lenth
, there is still one substring #include <string.h>
#include <stdio.h>
#include <stdbool.h>
bool isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
void substr(char str[], int low, int high)
{
printf("%.*s\n", high-low+1, (str+low));
}
int main(int argc, const char *argv[]) {
// ch-ch-ch-changes
char str[] = "auiouxaeibaou";
int length = strlen(str);
int start_index = 0, end_index = 0;
for (int x = 0; x < length; x++) {
char c = str[x];
if (isVowel(c) == false) {
end_index = x;
substr(str, start_index, end_index - 1 );
start_index = end_index + 1;
}
}
// ch-ch-ch-changes
end_index = length;
substr(str, start_index, end_index - 1 );
return 0;
}
Upvotes: 2