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;
Related
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
I was given a project where the user has to insert characters with some restrictions, but that is not the problem.
The problem is when the user presses "BACKSPACE" it should delete the last character stored in the array, but in my case it doesn't delete, it comes back so the user can replace it and that's not the goal here.
My code:
#include<stdio.h>
#include <string.h>
#include <locale.h>
main(){
int size = 0;
int pos = 0;
int var;
printf("Enter how many letters you want to write:");
scanf("%d", &size);
char text[size];
printf("Enter the characters:\n");
do{
var = getche();
if((var >= 65) && (var <= 90) || (var >= 192) && (var <= 221) || (var == 32)){
text[pos] = (char) var;
text[pos+1] = 0;
pos++;
}
else if (var == 8){
if (pos > 0){
text[pos] = 0;
text[pos] += " ";
pos--;
}
else{printf("Error");}
}
else{
text[pos] = 0;
system("cls");
printf("Enter the characters: \n");
printf("%s", text);
}
}while (pos< size);
printf("\n\nFinal string : ");
printf("%s", text);
}
All the help is much apreciated.
You have to clear the output and print again the new text when you the getche gets a backspace.
The code below performs what you are asking for, further improvement can be done, I'll leave that to you.
#include<stdio.h>
#include <string.h>
#include <locale.h>
int main(){
int size = 0;
int pos = 0;
int var;
printf("Enter how many letters you want to write:");
scanf("%d", &size);
char text[size];
printf("Enter the characters:\n");
do{
var = getche();
if((var >= 65) && (var <= 90) || (var >= 192) && (var <= 221) || (var == 32)){
text[pos] = (char) var;
text[pos+1] = 0;
pos++;
}
else if (var == 8){
if (pos > 0){
pos--;
text[pos] = 0;
system("cls");
printf("Enter the characters: \n");
printf("%s", text);
}
else{printf("Error");}
}
else{
text[pos] = 0;
system("cls");
printf("Enter the characters: \n");
printf("%s", text);
}
}while (pos< size);
printf("\n\nFinal string : ");
printf("%s", text);
}
This is my code. The second printf and getchar does not pop up after hitting enter for the first scanf. I am unsure why the second printf and getchar are not working and what the fix would be.
#include <stdio.h>
int main()
{
int choice;
int i;
i = 0;
int n;
n = 1;
int p;
char message[1000];
printf("Would you like to (1) encrypt or (2) decrypt?: ");
scanf("%i\n", choice);
printf("\nType your message: ");
message[p] = getchar();
for (i = 1; (i < 1000 && message[p] != '\0');) {
message[p] = message[p] + n;
n + 1;
}
for (i = 0; (i < 1000 && message[p] != '\0');) {
message[p] = message[p] - n;
n + 1;
}
return 0;
}
Your encription method works for encription and decription. This compiles and takes in a string including spaces modifies it and prints it out.
#include <stdio.h>
int main() {
int choice;
char temp;
int i = 0;
int n = 1;
int p, b;
char message[1000];
char endmessage[1000];
printf("Would you like to (1) encrypt or (2) decrypt?: ");
scanf("%i", &choice); // change as per comment above
scanf("%c", &temp); /* temp statement to clear buffer otherwise a second character has to be typed before the next print statement is executed */
printf("Type your message: "); //prompt added for user clarity
scanf("%[^\n]", message); /* this scanf line allows for spaces in the message */
printf("\nmessage typed is: %s \n", message);
p = (sizeof( message) + 1); /* strings need to have an ending '\0'
message[p] = '\0';
if (choice == 1) {
for (i = 0; (i < 1000 && message[i] != '\0');) {
endmessage[i] = (message[i] + n);
i +=1;
}
b = (sizeof( endmessage)); // replace encripted \0 with '\0'
endmessage[b] = putchar('\0');
printf("\nYour message encripted is: %s\n\n", endmessage);
}
if (choice == 2) {
for (i = 0; (i < 1000 && message[i] != '\0');) {
endmessage[i] = message[i] - n;
i += 1;
}
b = (sizeof(endmessage) -2); // replace decripted \0 with '\0'
endmessage[b] = putchar('\0');
printf("\nYour message decripted is: %s\n\n", endmessage);
}
}
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;
}
My program below conducts a Caesar's cipher in C. For some reason, after a message has been input by the user, the printf(" \nEnter key:") and
scanf("%d", &key) statements get "jumped" over. My thought was something related to the input buffer having a char and newline entered causing the jump (hence the fflush attempt). How do I prevent this behavior?
#include <stdio.h>
#include <stdlib.h>
int main() {
char message[50], ms;
int i, key, choice;
printf("Enter 1. to Encrypt, or 2. to Decrypt: ");
scanf(" %d", &choice);
printf("Enter a message to process: ");
scanf(" %c", message);
printf(" \nEnter key:");
fflush(stdin);
scanf("%d", &key);
for (i = 0; message[i] != '\0'; ++i) {
ms = message[i];
if (ms >= 'a' && ms <= 'z' && choice == 1) {
ms = ms + key;
if (ms >= 'a' && ms <= 'z' && choice == 2) {
ms = ms - key;
if (ms > 'z') {
ms = ms - 'z' + 'a' - 1;
}
}
message[i] = ms;
} else
if (ms >= 'A' && ms <= 'Z' && choice == 1) {
ms = ms + key;
if (ms >= 'A' && ms <= 'Z' && choice == 2) {
ms = ms - key;
}
if (ms > 'Z') {
ms = ms - 'Z' + 'A' - 1;
}
message[i] = ms;
}
if (choice == 1) {
printf(" \nEncrypted message: %s", message);}
else if (choice == 2) {
printf(" \nDecrypted message: %s", message);}
}
}
#ddisec I have noted 3 mistakes in your code .
First your scanf(" %c", message); .Here you have to use %s (String).
Second the result printing statements should be outside for-loop.
Third putting if(ms >= 'a' && ms <= 'z'&& choice == 2) inside if (ms >= 'a' && ms <= 'z' && choice == 1) dose not make any sense.
Try this corrected code:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
char message[50], ms;
int i, key, choice;
printf("Enter 1. to Encrypt, or 2. to Decrypt: ");
scanf("%d", &choice);
getchar(); // to handle unwanted newline.
printf("Enter a message to process: ");
scanf("%49[^\n]", message);
printf(" \nEnter key:");
scanf("%d", &key);
for (i = 0; message[i] != '\0'; ++i)
{
ms = message[i];
if (ms >= 'a' && ms <= 'z' && choice == 1)
{
ms = ms + key;
}
else if (ms >= 'a' && ms <= 'z' && choice == 2)
{
ms = ms - key;
}
else if (ms > 'z')
{
ms = ms - 'z' + 'a' - 1;
}
else if (ms >= 'A' && ms <= 'Z' && choice == 1)
{
ms = ms + key;
}
else if (ms >= 'A' && ms <= 'Z' && choice == 2)
{
ms = ms - key;
}
else if (ms > 'Z')
{
ms = ms - 'Z' + 'A' - 1;
}
message[i] = ms; // Only single modification code needed.
}
if (choice == 1)
{
printf(" \nEncrypted message: %s", message);
}
else if (choice == 2)
{
printf(" \nDecrypted message: %s", message);
}
}
There are multple problems in your code:
The way you enter the message is incorrect: scanf(" %c", message); reads a single byte into the message first element and does not even make it a C string. Use this instead, to read the message with embedded spaces:
scanf("%49[^\n]", message);
There are logic bugs in the rest of the code: for example, you test for choice == 2 inside the block that is only executed if choice == 1...
Here is a simplified version:
#include <stdio.h>
int main() {
char message[50];
int i, key, choice;
printf("Enter 1. to Encrypt, or 2. to Decrypt: ");
if (scanf("%d", &choice) != 1)
return 1;
printf("\nEnter a message to process: ");
if (scanf("%49[^\n]", message) != 1)
return 1;
printf("\nEnter key:");
if (scanf("%d", &key) != 1)
return 1;
for (i = 0; message[i] != '\0'; ++i) {
int ms = message[i];
if (ms >= 'a' && ms <= 'z') {
if (choice == 1)
ms = 'a' + ((ms - 'a') + key) % 26;
if (choice == 2)
ms = 'a' + ((ms - 'a') + 26 - key) % 26;
message[i] = ms;
} else
if (ms >= 'A' && ms <= 'Z') {
if (choice == 1)
ms = 'A' + ((ms - 'A') + key) % 26;
if (choice == 2)
ms = 'A' + ((ms - 'A') + 26 - key) % 26;
message[i] = ms;
}
}
if (choice == 1)
printf("\nEncrypted message: %s\n", message);
if (choice == 2)
printf("\nDecrypted message: %s\n", message);
}
return 0;
}