Why is does the tolower(*(text+i)); in my if statement not get executed when I run the program?
#include <stdio.h>
#include <ctype.h>
void vowel_caser(char text[])
{
int i = 0;
while(*(text+i) != '\0')
{
if (*(text+i) == 'A' || *(text+i) == 'E' || *(text+i) == 'I' || *(text+i) == 'O' || *(text+i) == 'U')
{
tolower(*(text+i));
}
i++;
}
}
int main()
{
char test[] = "This is An example";
vowel_caser(test);
puts(test);
return 0;
}
tolower returns the new value.
It does not modify the value in-place.
If you wish to modify the character, you need:
text[i] = tolower(text[i]);
Related
I am trying to create a program that removes the vowels from a sentence. However, my program keeps failing because it keeps printing zero byte in the string. Would anyone be able to show me where I am going wrong?
#include <stdio.h>
int remove_all_vowels(int character);
int main(void) {
int character = getchar();
while (character != EOF && character != '\0') {
int new_character = remove_all_vowels(character);
putchar(new_character);
character = getchar();
}
return 0;
}
int remove_all_vowels(int character) {
if (character == 'a' || character == 'e' || character == 'i' || character
== 'o' || character == 'u') {
return 0;
} else {
return character;
}
}
Your issue (outputting null characters) comes from the fact that you unconditionally putchar(3) the result of remove_all_vowels, which returns 0 (null character) when given character is a vowel.
To replace vowels with spaces:
You can simply change return 0; in remove_all_vowels to return ' ';
To completely remove vowels:
I would suggest having a function just to help you check against vowels rather than having it act like a transformation over a char, which is really pythonest.
Example of code:
int is_vowel(int character) {
return (
character == 'a' || character == 'e' || character == 'i'
|| character == 'o' || character == 'u'
);
}
// Then, in your main...
...
if (!is_vowel(character))
putchar(character);
your program works fine here
https://www.onlinegdb.com/online_c_compiler
Can give a screenshot whats the problem of your compiler?
#include <stdio.h>
#include <string.h>
int check_vowel(char);
int main()
{
char s[100], t[100];
int c, d = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for(c = 0; s[c] != '\0'; c++) {
if(check_vowel(s[c]) == 0) { // If not a vowel
t[d] = s[c];
d++;
}
}
t[d] = '\0';
strcpy(s, t); // We are changing initial string. This is optional.
printf("String after deleting vowels: %s\n", s);
return 0;
}
int check_vowel(char ch)
{
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
return 1;
else
return 0;
}
I am constructing a program that takes string input from the keyboard then shows the number of consonants as an output. I have managed to do it in a ridiculous way in the function count_consonants. I tested using if statement whether each character in the input is a number or symbol to ignore them during calculations. I originally wanted to check if the string is not a string using fgets but I don't know how. That's not an effective way, so any ideas for this?
#include <stdio.h>
#include <string.h>
//function to calculate the consonants
int count_consonants(char str[]) {
int idx;
for (idx = 0; idx < 100; ++idx) {
if (str[idx] == '\0') {
break;
}
}
int vowl = 0;
for (int i = 0; i < idx; ++i) { //loop to check if the characters are vowels or not
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o'
|| str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I'
|| str[i] == 'O' || str[i] == 'U' || str[i] == ' ') {
vowl += 1;
}
// numbers and symbols are counted here as vowels because if not,
// the compiler will count them the other way around
if (str[i] == '1' || str[i] == '2' || str[i] == '3' || str[i] == '4'
|| str[i] == '5' || str[i] == '6' || str[i] == '7' || str[i] == '8'
|| str[i] == '9') {
vowl += 1;
}
if (str[i] == ':' || str[i] == ',' || str[i] == '.' || str[i] == '$'
|| str[i] == '%' || str[i] == '^' || str[i] == '&' || str[i] == '*'
|| str[i] == '#' || str[i] == '_' || str[i] == '!') {
vowl += 1;
}
}
int cons = idx - vowl; // consonants = whole length of text - vowels
return cons - 1;
}
int main(int argc, char const *argv[]) {
char string[100];
char store[100][100];
int i = 0;
while (string[0] != '\n') {
fgets(string, 100, stdin);
strcpy(store[i], string);
i++;
}
for (int j = 0; j < i - 1; ++j) {
/* code */
printf("Number of consonants=%d\n", count_consonants(store[j]));
}
return 0;
}
shows the number of consonants
A simply way to count consonants, use isalpha(), strchr()
#include <string.h>
#include <ctype.h>
int my_isavowel(char ch) {
const char *p = strchr("aeiouAEIOU", ch); // search for a match
return p && *p; // If p is not NULL, and does not point to \0
}
int count_consonants(const char str[]) {
int count = 0;
while (*str != '\0') { // while not at end of string ...
char ch = *str++; // Get character and advance
count += isalpha((unsigned char) ch) && !my_isvowel(ch);
}
return count;
}
If you look for number of consonants, simply best count consonants instead of other things
#include <stdio.h>
#include <string.h>
int main (int narg,char*args[]){
char cons[ ] = "ZRTPQSDFGHJKLMWXCVBN";
char sentence[ ] = "This is my sentence!";
int i=0;
int sum_cons = 0;
for (i = 0; i < strlen(sentence); ++i)
if (strchr(cons,strupr(sentence)[i])) sum_cons++;
printf ("#CONS>%i\n",sum_cons);
return 0;
}
My assignment is to make a program that takes a string and output:
the number of characters in the string
the number of vowels in the string
the number of UPPERCASE letters in the string
the number of lowercase letters in the string
the number of other characters in the string
We are not allowed to use the ctype.h library.
Right now I'm just trying to output the number of vowels.
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
bool isVowel(char *c);
int main(){
char userString[5];
int i;
int vowelCount;
char *c;
printf("enter string:");
scanf("%c", userString);
for(i=0; i<= 4; ++i){
userString[i] = *c;
isVowel(c);
if(isVowel(c)){
vowelCount = vowelCount + 1;
}
}
printf("%d\n", vowelCount);
return 0;
}
bool isVowel(char *c){
if(*c == 'a' || *c == 'A' || *c == 'e' || *c == 'E' || *c == 'i' || *c
== 'I' || *c == 'o' || *c == 'O' || *c == 'u' || *c == 'U' ){
return true;
}
else{
return false;
}
}
I believe that isVowel is always returning false because, when I run it with the input "test!",I get this:
enter string: test!
0
You're not setting the c variable. I suspect this line:
userString[i] = *c;
should be something like:
c = userString + i;
This is your code with some corrections:
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
bool isVowel(char c);
int main(){
char userString[5];
int i;
int vowelCount=0;
char c;
printf("enter string:");
scanf("%s", userString); // %s
for(i=0; i<= 4; ++i){
c=userString[i] ;
printf("%c",c);
if(isVowel(c)){
vowelCount = vowelCount + 1;
}
}
printf("%d\n", vowelCount);
return 0;
}
bool isVowel(char c){
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' ||
c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U' ) ;
}
I'm having difficulties with a program I've been instructed to write. The program should search a word for the first vowel that appears, it then should print out the index of that vowel. If there are no vowel's, it should return -1.
This is my code so far:
int firstVowel(char* string){
//Variable for case with no vowels
int notInString = -1;
int length = strlen(string);
int i;
for(i = 0; i <= length; i+=1){
if(*string == 'a' || *string == 'e' || *string == 'i' ||
*string == 'o' || *string == 'u'){
return i;
}
else if(*string != 'a' || *string != 'e' || *string != 'i' || *string != 'o' ||
*string != 'u') {
return notInString;
}
}
}
When I run it with the input "abced" it returns 0 correctly. However, when I run it as fsed it returns -1 incorrectly.
You loop does not increment string and always returns after doing the first comparison, you want
int firstVowel(char* string) {
//Variable for case with no vowels
int notInString = -1;
int length = strlen(string);
int i;
for (i = 0; i <= length; i += 1) {
if (*string == 'a' || *string == 'e' || *string == 'i' ||
*string == 'o' || *string == 'u') {
return i;
} else {
string++;
}
}
return notInString;
}
This will search the entire array, returning the index of the first vowel it sees. It cannot return notInString until it has processed the entire string.
As stated in the title, I am dealing with a function that is called in main more than once, it's purpose is to count the vowels in an array of characters and the results are correct only the first time it gets called. After that instead of starting from zero, it picks up the counting from where it left off, leading to incorrect results.
This is the function:
int function(char *pointer, int num_of_elements)
{
int i=0;
if (num_of_elements==0) return i;
if ((*pointer== 'a') || (*pointer== 'A') || (*pointer== 'e') || (*pointer== 'E') || (*pointer== 'i') || (*pointer== 'I' )||( *pointer=='o') || (*pointer =='O') || (*pointer== 'u') || (*pointer== 'U')) {i++;}
pointer++;
num_of_elements--;
return function(pointer,num_of_elements);
}
The pointer points to the array of characters and the variable i is the counter.
You have to return the sum of iand the result of your recursive call of function. Adapt your code like this:
#include <ctype.h>
int function(char *pointer, int num_of_elements)
{
if (num_of_elements==0)
return 0;
char c = tolower( *pointer );
int i = ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) ? 1 : 0;
pointer++;
num_of_elements--;
return i + function( pointer, num_of_elements );
}
I recommend to solve your probelem with in a loop and to use function tolower:
#include <ctype.h>
int function(char *pointer, int num_of_elements)
{
int count = 0;
for ( int i = 0; i < num_of_elements; i++, pointer ++)
{
char c = tolower( *pointer );
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
count ++;
}
return count;
}