how can I make this run multiple times using do-while loop? - c

I'm trying to make a program that will scan the character I entered and will end only if the character inputted is a vowel or if the number of inputted characters has already reached 5
when I executed the code it only run once, and even if I inputted a vowel it does not terminate/end.
I tried adding c++ but it did not change anything
I feel like something is missing but I can't figure it out.
I've also tried watching tutorials about do-while loop.
here it is:
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c",&c);
do {
printf("%c",c);
} while (c == 'a' && c == 'e' && c == 'i' && c == 'o' && 'u' && c == 'A' && c == 'E' && c == 'I' && c == 'O' && 'U' && c == 5);
return 0;
}

Maintain a count and check if count < 5 and it is not any vowel
string ch ;
cin >> ch ;
int count = 1 ;
while(count < 5 && ch != "A" && ch!= "E" && ch != "I" && ch!= "O" && ch != "U" && ch != "a" && ch!= "e" && ch != "i" && ch!= "o" && ch != "u" ){
cout << ch << '\n';
cin>>ch;
count ++ ;
}

Related

I need help writing a Loop that finds vowels in C

I need to prompt the user for a letter. If that letter is not a vowel, keep prompting until a vowel is entered.
My desired outcome is:
Enter a vowel:z
That is not a vowel. Try again:h
That is not a vowel. Try again:w
That is not a vowel. Try again:t
That is not a vowel. Try again:o
Congrats. You picked a vowel.
So far my code is:
do
{
printf("Enter a vowel: ");
scanf(" %c", &letter);
}
while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
{
printf("That is not a vowel. Try again: ");
scanf(" %c", &letter);
}
printf("Congrats. You picked a vowel!!!");
Your braces, indentation and spacing are deceptive. It looks like the do has no while and that the while controls the block below it...
Try this:
/*...*/
while( 1 ) {
printf( "Enter a vowel: " );
char letter;
if( scanf( " %c", &letter ) != 1 )
exit( -1 );
if( strchr( "aeiouAEIOU", letter ) != NULL ) // found!
break;
printf( "That is not a vowel. Try again:\n\n" );
}
printf( "Congrats. You picked a vowel!!!\n" );
// NB: 'letter' has "gone out of scope".
/*...*/
strchr() searches the string supplied for a matching character, returning NULL if it is NOT found (ie: not a vowel, in this case.)
The "infinite loop" can only "break" when the user supplies a vowel.
Add a few LFs (\n) to the print statements.
Do and While should go together. So you are looking for a code that will be something like below.
Your objective should be to ensure the user is in loop till a vowel is received. Also you can further improve the code by using functions for the vowel check. Find a sample below.
do
{
printf("Enter a vowel: ");
scanf(" %c", &letter);
if (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U')
printf("Sorry, Try again \n");
} while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
printf("Congrats. You picked a vowel!!!");
}
The applicable loops in C are do-while, for and while. There is no do.
Hopefully OP will see the problem once OP's code is properly formatted.
do {
printf("Enter a vowel: ");
scanf(" %c", &letter);
} while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o'
&& letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I'
&& letter != 'O' && letter != 'U');
{
printf("That is not a vowel. Try again: ");
scanf(" %c", &letter);
}
There is no error message in the loop.
A more concise way to achieve that would be using strchr(), as suggested by #tadman in the comments.
#include <stdio.h>
#include <string.h>
int main () {
char * vowels = "aeiouAEIOU";
char letter;
do {
printf("Enter a vowel: ");
scanf(" %c", &letter);
} while (strchr(vowels, letter) == NULL);
printf("Congrats. You picked a vowel!!!");
}

While loop in C prints the same line more than once

char ch;
int nr=0;
printf("\n: ");
ch = getchar();
while(ch != 'q' && ch != 'Q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
printf("something");
ch = getchar();
}
printf("vocale: %d", nr);
its supposed to count the number of vowels until the user presses q or Q. it's such a silly program and yet i cant get past it.
Instead of using getchar
ch = getchar();
that also reads white space characters use scanf like
scanf( " %c", &ch );
Pay attention to the leading space in the format string. It allows to skip white space characters.
For example
while ( scanf( " %c", &ch ) == 1 && ch != 'q' && ch != 'Q'){
Also it will be more safer to write
ch = tolower( ( unsigned char )ch );
The problem is, that the input only gets flushed to your program whenever the user presses enter. Another reason why it seems not to work is, because you don't have a newline at the end of you output (printf("vocale: %d", nr); ), which causes the output not to be flushed to the terminal when the program ends. Fix this and your program works, but maybe not as you expect it to, because you still have to press enter. It will still only count to the first 'q' found.
int main() {
char ch;
int nr = 0;
printf(": ");
while(tolower(ch = getchar()) != 'q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
nr++;
}
printf("vocale: %d\n", nr);
}
The program:
: aeu q oi (here I pressed enter)
vocale: 3

How to delete characters except alphabets in c

There is a file "poem.txt":
*The ho$use cat sits.*
*And sm%iles and) sing&s.*
*He% know*(s a l_ot*
*Of s!ecret thi<ngs.*
I need to delete unnecessary symbols from it and write it to another file "poem_modified" without using arrays, functions, structures and pointer and only with <stdio.h> library:
I was able to do it so far:
#include <stdio.h>
int main() {
FILE *input;
FILE *output;
input = fopen ("poem.txt", "r");
output = fopen ("poem_modified.txt", "w");
if (input == NULL || output == NULL)
{
printf("Problem! \n");
return 1;
}
char ch ;
while((ch=getc(input)) != EOF)
fprintf(output, "%c", ch);
fclose(input);
fclose(output);
}
Adding conditions while printing the character can help
Suppose, it is required to include a-z and A-X only with spaces and newline char. So conditions can be made such as if the character is between a-z or between A-Z or it is newline or space, the char will be printed. Otherwise not. Any other conditions can be added.
The getc() function return type is an integer. documentation
Correct indentation helps to understand the code.
#include <stdio.h>
int main() {
FILE *input;
FILE *output;
input = fopen ("poem.txt", "r");
output = fopen ("poem_modified.txt", "w");
if (input == NULL || output == NULL)
{
printf("Problem! \n");
return 1;
}
int ch ;
while((ch=getc(input)) != EOF) {
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == ' ' || ch == '\n'){
fprintf(output, "%c", ch);
}
}
fclose(input);
fclose(output);
}
output :
The house cat sits
And smiles and sings
He knows a lot
Of secret things
Portability Concerns
Using the > and < operators on characters is not a portable solution for this in C. For the special case of the digits '0'...'9' you can do this because the C Standard specifies that these characters must be encoded contiguously and in ascending order.
It is unlikely that using comparison operators to check whether a character is alphabetic in the manner (ch >= 'A' && ch <= 'Z') will cause problems on most modern systems, but problems do occur. Certainly it could be a problem on older systems, such as legacy systems installed at institutions many years ago. This is exactly why the functions described in ctype.h should usually be preferred: these can be relied upon to work portably.
Portable Solutions
But if this is not possible more portable solutions than the aforementioned char comparison which relies upon a particular character encoding can be had.
Being unable to use arrays is a severe (and artificial) constraint. Of the two solutions below, the first solution does use an array (keepers) to encode characters which should be written to output. There is another solution following which does not use such an array, and I think that it meets all of OP's requirements, yet the second solution is a bit more awkward and error-prone to write.
Both solutions are more portable than using (ch >= 'A' && ch <= 'Z') methods, and both give the same results:
$ cat poem_modified.txt
The house cat sits
And smiles and sings
He knows a lot
Of secret things
Using an Array
The first solution defines an array keepers which is initialized by a string literal to contain all characters which should be written to output. As characters are read from input, the program checks in keepers to see if the character is present in this list; if so it is written to output, and if not the next character is read from input.
#include <stdio.h>
int main(void) {
// Open input file and check for errors
const char *input_file = "poem.txt";
FILE *input = fopen(input_file, "r");
if (input == NULL) {
fprintf(stderr, "Unable to open file %s for input\n", input_file);
return 1;
}
// Open output file and check for errors
const char *output_file = "poem_modified.txt";
FILE *output = fopen(output_file, "w");
if (output == NULL) {
fprintf(stderr, "Unable to open file %s for output\n", output_file);
fclose(input);
return 1;
}
char keepers[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ \n";
for (int ch = fgetc(input); ch != EOF; ch = fgetc(input)) {
// Is `ch` an alphabetic character?
size_t idx = 0;
char keeper = keepers[idx];
while(keeper != '\0') {
if (ch == keeper) {
putc(ch, output);
break;
}
keeper = keepers[++idx];
}
}
fclose(input);
fclose(output);
return 0;
}
Using Brute Force
The second solution does the same thing as the first, but without the array. Here instead of using an array to hold the list of characters which should be kept, an if statement with a very long conditional expression encodes this information.
#include <stdio.h>
int main(void) {
// Open input file and check for errors
const char *input_file = "poem.txt";
FILE *input = fopen(input_file, "r");
if (input == NULL) {
fprintf(stderr, "Unable to open file %s for input\n", input_file);
return 1;
}
// Open output file and check for errors
const char *output_file = "poem_modified.txt";
FILE *output = fopen(output_file, "w");
if (output == NULL) {
fprintf(stderr, "Unable to open file %s for output\n", output_file);
fclose(input);
return 1;
}
for (int ch = fgetc(input); ch != EOF; ch = fgetc(input)) {
// Is `ch` an alphabetic character, space, or newline?
if (ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'e'
|| ch == 'f' || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'j'
|| ch == 'k' || ch == 'l' || ch == 'm' || ch == 'n' || ch == 'o'
|| ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 't'
|| ch == 'u' || ch == 'v' || ch == 'w' || ch == 'x' || ch == 'y'
|| ch == 'z' || ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D'
|| ch == 'E' || ch == 'F' || ch == 'G' || ch == 'H' || ch == 'I'
|| ch == 'J' || ch == 'K' || ch == 'L' || ch == 'M' || ch == 'N'
|| ch == 'O' || ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S'
|| ch == 'T' || ch == 'U' || ch == 'V' || ch == 'W' || ch == 'X'
|| ch == 'Y' || ch == 'Z' || ch == ' ' || ch == '\n')
{
putc(ch, output);
}
}
fclose(input);
fclose(output);
return 0;
}
You can use the various functions from ctype.h to check if something belongs to a certain category of symbols. For example isalpha checks if a character is a letter and isspace checks if it's a space or new line character etc. By using these two functions in combination, we can chose to only print characters that are either letters or spaces. Example:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main (void)
{
char input[] = "*The ho$use cat sits.*\n"
"*And sm%iles and) sing&s.*\n"
"*He% know*(s a l_ot*\n"
"*Of s!ecret thi<ngs.*\n";
size_t length = strlen(input);
for(size_t i=0; i<length; i++)
{
if(isalpha(input[i]) || isspace(input[i]))
{
putchar(input[i]);
}
}
}
Apart from the ctype.h functions making the code easier to read, manual checks like ch >= 'A' && ch <= 'Z' are strictly speaking not well-defined or portable. Because C doesn't guarantee that letters are placed adjacently in the symbol table (see for example the EBCDIC, which was a format used in the Jurassic era). Also the ctype.h functions might handle "locale-specific" characters outside the classic 7 bit ASCII.

c how to check the first char of an array

I am trying to take in 10 characters over a serial console and add them to an array called buffer. The first character needs to be 'L' or 'S' and the next characters either '1' or '0'.
The code passes the first if statement ok. But the line if((buffer[0] != 'L') || (buffer[0] != 'S')) doesn't seem to work even when I enter 'L' OR 'S'.
Is there anything wrong with using the buffer[0] != notation?
int main(void)
{
char ch;
char buffer[10] = "";
putstring("Enter 9 characters beginning with 'L' or 'S' and 8 digits\r\n");
for (int i = 0; i < 9; i++) {
ch = getcharacter();
if ((ch == '0') || (ch == '1') || (ch == 'L') || (ch == 'S')) {
buffer[i] = ch;
//check first character
if((buffer[0] != 'L') || (buffer[0] != 'S')) {
printf("First letter must be L or S\r\n");
goto error;
}
error:
return -1;
}
}
}
int getcharacter(void) {
char c = 0;
const uint32_t recieve_ready = 1 << 7;
//disable interrupt for a read ready
*uart_control_reg = 0;
while (1) {
//check if RRDY bit is set
if ((*uart_status_reg) & recieve_ready) {
c = *uart_rxdata_reg;
break;
}
}
return ((char) c);
}
if((buffer[0] != 'L') || (buffer[0] != 'S'))
is wrong, you need
if((buffer[0] != 'L') && (buffer[0] != 'S'))
or
if (!(buffer[0] == 'L' || buffer[0] == 'S'))
Your original code was "if the char is not L or the char is not S" which is always true. If the char is L, then the second part was true, making the whole if statement true.
Just noticed Chris Turner's comment above. The return -1 is always executed, move it to replace the line that says goto error.
Try using
if((buffer[0] != 'L') && (buffer[0] != 'S'))
instead of
if((buffer[0] != 'L') || (buffer[0] != 'S'))
Just some logic problem here. According to your code, the char needs to be equal to 'L' AND 'S' to avoid the condition, which is never the case !

I'm learning C language but I cannot run the 'y' to continue the program

Here i get the answers for vowels but not for continue it says error for the continue and also for break.
#include < stdio.h >
main() {
char c, d;
printf("say your word to find the vowels\n");
scanf("%c", & c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("continue\n");
printf("(y/n)\n");
scanf("%c", & d);
if (d == 'y' || d == 'Y')
continue;
else
break;
return 0;
}
if (d == 'y' || d == 'Y')
continue; // where?
else
break; // what?
You have nothing to continue or break in your main function.
Both continue and break only make sense within a loop, so you should add a while(true) loop around the code in main() to repeat the whole thing until the user decides to quit.
Continue and break works in the loop block.. but here you have not added them in loop. Put the whole if block in the while(1) and it will work
#include <stdio.h>
int main()
{
char c;
int d;
while (1) {
printf("say your word to find the vowels\n");
scanf("%c", &c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("To continue enter 1\n");
scanf("%d", &d);
if (d == 1)
continue;
else
break;
}
return 0;
}
Check the continue and break statement syntax in the following link.
https://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue

Resources