Reputation: 191
I have a problem in which I have to take 3 words from user as input. What I have to do with that input:
Here is the code I tried:
#include <stdio.h>
#include <string.h>
int main() {
char first[20], second[20], third[20];
int i, j;
char vowel[5] = { 'a', 'e', 'i', 'o', 'u' };
printf("Enter first word: ");
scanf("%s", first);
printf("Enter second word: ");
scanf("%s", second);
printf("Enter third word: ");
scanf("%s", third);
for (i = 0; i < strlen(first); i++) {
for (j = 0; j < 5; j++) {
if (first[i] == vowel[j])
first[i] = '$';
}
}
printf("Final strings are: \n");
printf("%s", first);
for (i = 0; i < strlen(second); i++) {
if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'e');
second[i] = '#';
}
printf("%s", second);
printf("%s", strupr(third));
}
NOTE: All 3 words should be concatenated on output screen
Output:
Enter first word: kali
Enter second word: kali
Enter third word: kali
Final strings are:
k$l$####KALI
But the expected output is:
Enter first word: kali
Enter second word: kali
Enter third word: kali
Final strings are:
k$l$#a#iKALI
What I am doing wrong?
Upvotes: 2
Views: 137
Reputation: 145287
You made a silly mistake in this statement:
if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'u');
second[i] = '#';
You added an extra ;
at the end of the if
line, making the test useless and the following statement second[i] = '#';
execute unconditionally.
You should break such long expressions on multiple lines, avoid redundant tests and use {
and }
if (second[i] != 'a' && second[i] != 'i' && second[i] != 'o' &&
second[i] != 'u') {
second[i] = '#';
}
Upvotes: 3
Reputation:
#include <stdio.h>
#include <string.h>
int main() {
char first[20], second[20], third[20];
int i, j;
char vowel[5] = { 'a', 'e', 'i', 'o', 'u' };
printf("Enter first word: ");
scanf("%s", first);
printf("Enter second word: ");
scanf("%s", second);
printf("Enter third word: ");
scanf("%s", third);
for (i = 0; i < strlen(first); i++) {
for (j = 0; j < 5; j++) {
if (first[i] == vowel[j])
first[i] = '$';
}
}
printf("Final strings are: \n");
printf("%s", first);
for (i = 0; i < strlen(second); i++) {
if (second[i] != 'a' && second[i] != 'e' && second[i] != 'i' && second[i] != 'o' && second[i] != 'u') // You mistakenly put a semicolon here
second[i] = '#';
}
printf("%s", second);
printf("%s", strupr(third)); }
Upvotes: 0
Reputation: 31419
There are lot of problems with your code. If you turn on compiler warnings you'll see this:
$ clang -Wall -Wextra -std=c11 -pedantic-errors b.c
b.c:32:109: warning: if statement has empty body [-Wempty-body]
...!= 'i' && second[i] != 'o' && second[i] != 'u' && second[i] != 'u');
^
b.c:32:109: note: put the semicolon on a separate line to silence this warning
b.c:37:18: warning: implicit declaration of function 'strupr' is invalid in C99
[-Wimplicit-function-declaration]
printf("%s", strupr(third));
^
b.c:37:18: warning: format specifies type 'char *' but the argument has type
'int' [-Wformat]
printf("%s", strupr(third));
~~ ^~~~~~~~~~~~~
%d
b.c:20:18: warning: comparison of integers of different signs: 'int' and
'unsigned long' [-Wsign-compare]
for(i = 0; i < strlen(first); i++){
~ ^ ~~~~~~~~~~~~~
b.c:31:18: warning: comparison of integers of different signs: 'int' and
'unsigned long' [-Wsign-compare]
for(i = 0; i < strlen(second); i++){
~ ^ ~~~~~~~~~~~~~~
5 warnings generated.
/tmp/b-8f1874.o: In function `main':
b.c:(.text+0x22a): undefined reference to `strupr'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If you google the warning messages you'll get good hints about what to do about them. The warning about the semicolon that makes the if
statement having an empty body is what causes your problems.
Another problem is that you don't check the return code of scanf
to see if the read was successful. It will return the number of successful reads.
Avoid using strupr
. It is a deprecated non-standard function.
Upvotes: 2