This simple program will change your view towards string manipulations using pass by reference.
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 43 44 45 46 47 48 49 50 51 52 |
/* count the number of vowels, consonants, digits , whitespace characters, and '"other" characters in a line of text */ #include <stdio.h> #include <ctype.h> #include<string.h> /* function prototype */ void scan_line(char line[], int *pv, int *pc, int *pd, int *pw, int *po); //main implementation int main(void) { char line[80] ; /* line of text */ int vowels = 0 ; /* vowel counter */ int consonants = 0; /* consonant counter */ int digits = 0; /* digit counter */ int whitespc = 0; /* whitespace counter */ int other = 0; /* remaining character counter */ printf("Enter a line of text below: \n") ; gets(line); scan_line(line, &vowels, &consonants, &digits, &whitespc, &other); printf( "\n No . of vowels: %d ", vowels); printf("\n No. of consonants: %d", consonants); printf("\n No. of digits : %d", digits) ; printf("\n No . of whitespace characters: %d", whitespc); printf("\n No. of other characters: %d", other); return 0; } void scan_line(char line[] , int *pv, int *pc, int *pd, int *pw, int *po) /* analyze the characters in a line of text */ { char c; /* uppercase character */ int count = 0; /* character counter */ while((c =toupper(line[count])) != '\0' ) { if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { ++ *pv; /* vowel */ } else if ( c >= 'A' && c <= 'Z' ){ ++ *pc; /* consonant */ } else if ( c >= '0' && c <= '9'){ ++ *pd; /* digit */ } else if ( c == ' ' || c == '\t' ){ ++ *pw; /* whitespace */ } else{ ++ *po; /* other */ } ++count;//increase the position of the index in the string }// loop }//function ends |
Leave a Reply
You must be logged in to post a comment.