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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// This is an old example from 2/28/2024 to illustrate the use of *a_pos.
// Okay to copy/adapt if you understand BUT not likely to be helpful on HW09.
/*
The rabid panther roller skates through malt shakes.
▲
pos
*/
const char END_CHAR = '\0';
bool is_vowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
char find_next_vowel(char** a_pos) {
while(!is_vowel(**a_pos)) {
if(**a_pos == '\0') {
return END_CHAR;
}
*a_pos += 1; // ← IMPORTANT PART - same concept as homework but slightly diff code
}
char vowel = **a_pos;
*a_pos += 1; // put cursor in position we will want for next time
return vowel;
}
int main(int argc, char* argv[]) {
char* s = "The rabid panther roller skates through malt shakes.";
char* pos = s; // same as: char* pos = s;
for(char ch = find_next_vowel(&pos); ch != END_CHAR; ch = find_next_vowel(&pos)) {
printf("Next vowel: '%c' Rest of string: \"%s\"\n", ch, pos);
}
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.