Do not want to print multiple statement - c

I don't want my code to print other statements when the statement "is not a letter of the alphabet" is printed. Please help! I am still a noobie.
#include <stdio.h>
int main()
{
char ch;
int int_ch;
do {
printf("Type in an alphabet letter:");
scanf("%c%*c", &ch);
int_ch = (int)ch;
printf("Ch has ascii value %d\n", ch);
if ((ch >= 'a') && (ch <= 'z'))
{
int_ch = int_ch - 32;
}
else if ((ch >= 'A') && (ch <= 'Z'))
{
int_ch = int_ch + 32;
}
else
{
printf("Is not a letter of the Alphabet.");
}
//THIS HERE
ch = (char)int_ch;
printf("Ch is now %c\n", ch);
printf("Ch is now ascii value %d\n", int_ch);
} while (ch != '#');
return (0);
}

add continue is this else-statement
else
{
printf("Is not a letter of the Alphabet.");
continue;
}
The continue statement skips the current iteration of the loop and continues with the next iteration.

Insert the statement continue; after the printf() statement:
printf("Is not a letter of the Alphabet.");
continue;
which will let the program flow pass directly to the condition of the do while-loop:
Corrected code:
#include <stdio.h>
int main()
{
char ch;
int int_ch;
do {
printf("Type in an alphabet letter: ");
scanf("%c%*c", &ch);
int_ch = (int)ch;
printf("Ch has ascii value %d\n", ch);
if ((ch >= 'a') && (ch <= 'z'))
{
int_ch = int_ch - 32;
}
else if ((ch >= 'A') && (ch <= 'Z'))
{
int_ch = int_ch + 32;
}
else
{
printf("Is not a letter of the Alphabet.\n\n");
continue;
}
ch = (char)int_ch;
printf("Ch is now %c\n", ch);
printf("Ch is now ascii value %d\n", int_ch);
} while (ch != '#');
return (0);
}
Execution:
Type in an alphabet letter: 7
Ch has ascii value 55
Is not a letter of the Alphabet.
Type in an alphabet letter: (Waiting for data for another iteration)

Related

Something wrong with %lf in C [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
I'm doing homework and I have no idea why the %lf selector isn't working. I have to take a line of characters and determine if they can be a floating point or whole number and then print that number. Here's the code:
#include <stdio.h>
int main() {
char ch;
int isNumber = 1, dot = 0, negativeMult = 10;
double result = 0;
printf("\nEnter characters: ");
scanf("%c", &ch);
while (ch != 10) {
if ((ch >= '0' && ch <= '9')) {
if (dot) {
result = result + (ch - '0') / negativeMult;
negativeMult *= 10;
} else {
result = result * 10 + (ch - '0');
}
} else
if (ch == '.')
if (dot)
isNumber = 0;
else
dot = 1;
else {
isNumber = 0;
break;
}
scanf("%c", &ch);
}
if (isNumber)
printf("\nThe number is %lf", result);
else
printf("\nEntered characters are not able to be a number.");
return 0;
}
Edit: I forgot output. Sorry.
Input: Enter characters: 123.648
Output: The number is 123.000000
the error is here:
result = result + (ch - '0') / negativeMult;
(ch - '0') / negativeMult is integer division and it is always 0
it has to be
result = result + (double)(ch - '0') / negativeMult;
some more small errors amendments:
int main(void)
{
char ch;
int isNumber = 1, dot = 0, negativeMult = 10;
double result = 0;
int scanfresult;
printf("\nEnter characters: ");
scanfresult = scanf("%c", &ch);
while (ch != '\n' && scanfresult == 1)
{
if ((ch >= '0' && ch <= '9'))
{
if (dot)
{
result = result + (double)(ch - '0') / negativeMult;
negativeMult *= 10;
}
else
{
result = result * 10 + (ch - '0');
}
}
else if (ch == '.')
if (dot)
isNumber = 0;
else
dot = 1;
else
{
isNumber = 0;
break;
}
scanfresult = scanf("%c", &ch);
}
if (isNumber)
printf("\nThe number is %f", result);
else
printf("\nEntered characters are not able to be a number.");
return 0;
}
https://godbolt.org/z/nTKdjYsz8

C programm to read characters between two '*'

I want the programm to read the characters that are between two stars and if there are not two stars, it must print a respective message. For example if the input is 1abc*D2Efg_#!*34567, the output is between first tow stars (letters : 4, digits:1, other:3) any help will be appreciated
int main()
{
int ch,lowercase_lett,digits,other,uppercase_lett,asterisk;
lowercase_lett = 0;
uppercase_lett = 0;
digits = 0;
other = 0;
asterisk = 0;
printf("enter characters : ");
while((ch = getchar()) != '\n' && ch != EOF)
{
if(ch == '*')
{
asterisk++;
}
if(asterisk < 2)
{
printf("\ntwo asterisks not found\n");
}
else
{
if(ch>='a' && ch <= 'z')
{
lowercase_lett++;
}
else if(ch>='A' && ch <= 'Z')
{
uppercase_lett++;
}
else if(ch >='0' && ch <= '9')
{
digits++;
}
else
{
other++;
}
}
}
printf("\n%d letters %d digits and %d other" , lowercase_lett+uppercase_lett,digits,other);
return 0;
}
Count characters when exactly one asterisk has been found. Functions in ctype.h are useful to determine the type of characters.
#include <stdio.h>
#include <ctype.h>
int main(void){
int ch,lowercase_lett,digits,other,uppercase_lett,asterisk;
lowercase_lett = 0;
uppercase_lett = 0;
digits = 0;
other = 0;
asterisk = 0;
printf("enter characters : ");
while((ch = getchar()) != '\n' && ch != EOF)
{
if(ch == '*')
{
asterisk++;
if(asterisk>=2)
{
break;
}
}
else if(asterisk==1)
{
if(islower(ch))
{
lowercase_lett++;
}
else if(isupper(ch))
{
uppercase_lett++;
}else if(isdigit(ch)){
digits++;
}else{
other++;
}
}
}
if(asterisk<2)
{
printf("\ntwo asterisks not found\n");
}
else
{
printf("\n%d letters %d digits and %d other" , lowercase_lett+uppercase_lett,digits,other);
}
return 0;
}
You were almost there, but your code contains some lines in wrong places.
Look at my solution. I just tested and it works for your problem:
#include <stdio.h>
int main(){
int ch;
int lowercase_lett = 0;
int uppercase_lett = 0;
int digits = 0;
int other = 0;
int asterisks = 0;
printf("Enter characters: ");
while((ch = getchar()) != '\n' && ch != EOF)
{
if(asterisks == 2){
break; //End the search
}
else if(ch == '*'){
asterisks++; //Increment until we have 2 *
}
else if(asterisks == 1){
if((ch >= 'a') && (ch <= 'z')){
lowercase_lett++;
}
else if((ch >='A') && (ch <= 'Z')){
uppercase_lett++;
}
else if((ch >= '0') && (ch <= '9')){
digits++;
}
else{
other++;
}
}
}
if (asterisks >= 2){
printf("\n%d letters %d digits and %d other" , lowercase_lett+uppercase_lett,digits,other);
}
else{
printf("\ntwo asterisks not found\n");
}
return 0;
}

Getting Input and exiting do while loop in C

I'm doing an exercise where i ask the user for an input and then i cypher de input.
Bellow the code:
int main(int argc, char** argv) {
char string[50], cypher[50];
int num, i;
do {
printf("Enter text to cypher: \n");
scanf(" %s", string);
do {
printf("Enter cypher number: ");
scanf(" %d", &num);
while (num > 25) // only 25 letters, get number between >25
{
num -= 25;
}
} while (num <= 0);
for (i = 0; string[i] != '\0'; i++) {
cypher[i] = string[i] + num;
if (cypher[i] > 90 && cypher[i] < 97) // upper case
{
cypher[i] = cypher[i] - 'Z' + 'A' - 1;
}
if (cypher[i] > 122) //lower case
{
cypher[i] = cypher[i] - 'z' + 'a' - 1;
}
}
cypher[i] = '\0';
printf("%s\n", cypher);
}
while (string[0] != '0');
}
I want to exit the do while loop if the user input is '0' when asked to "Enter text to cypher: " without showing the next menssage "Enter cypher number".
Thank you in advance for your help.
after first scanf you should write something like if(string[0] == '0') break;

How to use the switch condition in order to get the upper case and lower case of the input string

i want a solution. is it possible that if i press 1 and get the upper case of the string and when i press 2, then got the lower case if the string via using the switch condition or something else.i just started coding and new to this field.
i tried to do this thing with function, but maybe due to lake of knowledge, id did nit get the result.
int main()
char str[100];
int i;
// printf("Enter the string: \n");
// gets(s);
switch (case)
int main()
{
int case;
printf("Enter the string: \n");
scanf("%d", &case);
gets(str);
switch(case)
{
case 1:
for(i = 0; str[i] != '\0' ; i++)
if(str[i] >= 'a' && s[i] <= 'z')
str[i] = str[i] - 32;
printf("\n The string's upper case = %s", str);
break;
case 2:
for(i = 0; str[i] != '\0' ; i++)
if(str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
printf("\n The string's lower case = %s", str);
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
return 0;
}
m expecting when i press 1 then get the upper case and when i press 2 i get lower case in string.
1
hello world
2
HELLO WORLD
i want to do it with the switch.
Your above code is a real mess and the absolute chaos. Read something about C and rewrite the code.
I have created a simple example for you which handles the first part of your task. This example doesn´t contain any error handling and without the second part. You can add it at your own if you understand the function of the code and how to write C.
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
printf("Enter the string:\n\r");
scanf("%s", str);
printf("Plese enter the case:\n\r");
scanf("%d", &i);
switch(i)
{
// Upper case
case 1:
{
// Loop over each char
for(i = 0; str[i] != 0; i++)
{
// Replace the lower case chars
if((str[i] >= 'a') && (str[i] <= 'z'))
{
str[i] = toupper(str[i]);
}
}
break;
}
// Lower case
case 2:
{
// Your task
break;
}
}
printf("%s\n\r", str);
return 0;
}
Your Logic seems to be correct however, your syntax is apparently wrong at some places. The following code is your code with correct syntax. Compare this with the code that you have posted. :)
#include<stdio.h>
int main()
{
//Declaring variables
char str[100];
int i;
int cas;
//Taking string input
printf("Enter the string: \n");
scanf("%s",str);
printf("Enter Option 1 for Uppercase and Option 2 for Lowercase");
scanf("%d",&cas);
//Using Switch Case
switch(cas)
{
case 1:
for(i = 0; str[i]!='\0' ; i++)
if(str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
printf("\n The string's upper case = %s \n", str);
break;
case 2:
for(i = 0; str[i] != '\0' ; i++)
if(str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
printf("\n The string's lower case = %s \n", str);
break;
default: printf("Choice other than 1 and 2");
}
return 0;
}

How to avoid printing the ASCII value of '\n' in a while loop?

My task is to get an input, print out the character and the ASCII value, and to present them each 8 for 1 line. For every input I'm typing I'm getting also the value of the newline character and I don't want to print it.
This is my code:
#include <stdio.h>
int main()
{
char ch;
int count = 0;
printf("please type an input:\n");
while ((ch = getchar()) != '#')
{
++count;
printf("%c=%d ", ch, ch);
if (count%8 == 0) printf("\n");
}
}
You can use another getchar() right after reading the first one:
while ((ch = getchar()) != '#')
{
getchar(); // To eat the newline character
// Rest of code
}
Or you can use scanf() and rewrite the loop equivalently:
while (scanf(" %c", &ch)==1)
{
if(ch != '#')
{
++count;
printf("%c=%d ", ch, ch);
if (count%8 == 0)
printf("\n");
}
}
int main()
{
char ch;
int count = 0;
printf("please type an input:\n");
while (1) {
ch = getchar();
if (ch == '#') break;
if (ch == '\n') continue;
printf("%c=%d ", ch, ch);
if (!(++count%8)) printf("\n");
}
}

Resources