c isalpha and isdigit while loop - c

How do I put isalpha and isdigit in a while(1) loop?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
char type[256];
printf("You can type a number or a word. Type exit to exit! \n");
printf("Type: ");
fgets (type, 256, stdin);
if (isalpha(type[i]))
{
printf("Typed text: %s\n", type);
if((strcmp(type,"exit\n") == 0))
{
printf("Exiting...\n");
exit(1);
}
}
else if (isdigit(type[i]))
{
printf("Typed number: %s\n", type);
}
else
{
printf("Typed: %s\n", type);
printf("Its not a letter or number...?!\n");
}
}
I tried adding while(1) at the start at the code and close it at the end of code, but as soon as I enter number or letter the console crashes... Could someone please help me with this?

Your problem is not a loop problem, you need to give a value to i , as it is undefined and you get a nice crash. Please replace
int i;
with
int i=0;

Related

Debug assertion failed, Expression: (unsigned)(c + 1) <=256

What I am trying to do:
Make a program that opens a file and read the content (mostly characters)
Example on what it should do:
The file contains "ABA", It should print: Letter A Count 2 || Letter B Count 1
but I am getting this error when I start debugging http://imgur.com/a/zvpWg
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct arr
{
char letter;
int count;
};
void main ()
{
arr s[7];
char letter;
FILE *fp;
fp=fopen("D:\\data.txt","r");
if(fp==NULL)
{
printf("Error File Not Found\n");
getch();
exit(1);
}
while(fscanf(fp,"%ch",&letter)!=EOF)
{
for(int i=0;i<7;i++)
{
if(!isalpha(s[i].letter))
{
s[i].letter=letter;
s[i].count=1;
break;
}
else if(s[i].letter == letter)
s[i].count++;
break;
}
}
for(int h=0;h<7;h++)
printf("Letter: %c Count: %d ||",s[h].letter,s[h].count);
getch();
}
Where am i wrong exactly ?
The Problem might be -
function isalpha() is being called with an integer whose value is not a single byte i.e. 0-255.

Mono alphabetic cipher doesnt work correctly

Here is my code. Substitution Cipher in C. But i got an error this line: char *encryption (char cipher_text[]) { function definition is not allowed here. I think probably "main" function place not right. How can i fix it?
And by the way how can i generate random alphabet for this code? Thank you so much.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
char *encryption (char cipher_text[]) {
int i, val, j;
printf("\n abcdefghijklmnopqrstuvwxyz \n");
You cannot define a function inside another one. encryption is defined in main :/
In C, you cannot declare a function inside another function, like you did.
Here is your code that will compile:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
char *encryption (char []);
void *decryption (char []);
char alpha [26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char key[26];
char *encryption (char cipher_text[]) {
int i, val, j;
printf("enter the unique KEY of 26 character:");
scanf("%s", key);
printf("\n abcdefghijklmnopqrstuvwxyz \n");
printf ("%s", key);
for (i=0; i <strlen(cipher_text); i++)
{
for (j=0; j<26; j++)
{
if (alpha[j]== cipher_text[i]) {
cipher_text[i]=key[j];
break;
}
}
}
printf ("your message enc: %s", cipher_text);
return cipher_text;
}
int main ()
{
int i, key, choice, flag=0;
char *c_text, msg[255];
printf("\n Enter plain text:");
scanf ("%[^\n]", msg);
encryption(msg);
return 0;
}
How to generate random characters is answered here.

how do i do infinite loop in this function in C

i have a question, my loop is stopping at sequence 2, i want to infinite loop the function ambil_nilai() and ulang() until scanf receiving the word "tidak" and then the program stop, and it seems i can't quite right to do it, please please help me, and please tell me if there's any not quite right in my ode, thank you very much for your help.
#include <stdio.h>
int ambil_nilai(){
int nilai, NMK;
printf("Masukkan mata kuliah yang ingin dicari analisa nya:\n");
scanf("%d",&NMK);
printf("Masukkan nilai mata kuliahnya:\n");
scanf("%d",&nilai);
if(nilai<=50){
printf("kamu harus belajar lagi karena nilai kamu kurang\n\n");
}
else if(nilai>=51){
printf("nilai kamu sudah cukup untuk lulus mata kuliah\n\n");
}
return 0;
}
char ulang(){
char lagi='y';
char tidak='n';
printf("ingin coba mata kuliah lain? tekan y untuk yes, n untuk no\n");
scanf("%c %c", &lagi,&tidak);
if(lagi){
system("clear");
return ambil_nilai();
}else if(tidak){
printf("terima kasih sudah menggunakan program ini\n");
}
return 1;
}
int main()
{
printf("\n\n");
printf("ini adalah mata kuliah kamu:\n");
printf("1. A\n");
printf("2. B\n");
printf("3. C\n\n");
ambil_nilai();
ulang();
printf("\n\n");
return 0;
}
I just want to correct matt93 code with an explanation :
First, you malloc a fixed size, so that's not really useful. Plus, you didn't check if malloc doesn't return NULL.
Moreover, your scanf is flawed, since we can easily do a buffer overflow.
In addition, you doesn't flush stdin, so when the loop continu, you will have a behavior in your program that you don't really want.
Here the code that correct all of this :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_HELPER(str) #str
#define STR(str) STR_HELPER(str)
#define ARRAY_LEN 5
int main(void)
{
char string[ARRAY_LEN + 1];
int c;
do {
printf("Enter 'tidak' please\n");
scanf("%"STR(ARRAY_LEN)"[^\n]", string);
while ((c = getchar()) != '\n' && c != EOF);
printf("Got : '%s'\n", string);
} while (strcmp(string, "tidak") != 0);
return (0);
}
You can read this for STR_HELPER and STR macro.
Don't forget to read scanf man if you have another question.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *string = (char*) malloc(60, sizeof(char));
while (scanf("%s", string) > 0) {
if (strcmp(string, "tidak") == 0) break;
}
let me know, if this was what you were looking for

How to set a string to perform a function or command

I kind of have a basic question for you because it's driving me crazy. How do I go about writing my functions to specific strings? Like, if I was creating a while loop and wanted the program to end, how would I write it so that the program itself ends when I type in "end" when it asks for input?
EDIT: Alright, so I figured out pretty easily how to end my function by typing "end", but now for some reason depending on how many sentences I write, my program keeps repeating itself.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int i;
char buf[10];
printf("Command? ");
scanf("%s", buf);
while(buf != "end")
{
if(strcmp(buf, "end")== 0){
break;
}
switch( buf[i] ){
//Where the cases will inevitably go
default:
puts("I'm sorry, but that's not a command.\n");
break;
}
printf("Command? ");
scanf("%s", buf);
}
puts("End of Program.");
getch();
}
char *myInputString = NULL;
while (1) {
/* read in myInputString from user input, and test... */
if (strcmp(myInputString, "foo") == 0)
break;
}
return EXIT_SUCCESS;

How to format input to only accept integer values

input value 123 -- this value is integer, and valid
input value 1b23a -- this value is invalid
How do I detect which values are valid and not?
Here is my code:
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
Please see this answer regarding use of strtol(). It is a safe way to convert arbitrary input that should be a string representation of an integer, while also saving 'garbage' bytes for additional analysis.
Using it, your code would look something like this:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef LINUX_VERSION
#include <curses.h>
#else
#include <conio.h>
#endif
#define BUFF_SIZE 1024
int main(void)
{
char str1[BUFF_SIZE], *garbage = NULL;
long num = 0;
printf("Enter the Number ");
scanf("%s",str1);
errno = 0;
num = strtol(str1, &garbage, 0);
if (errno) {
printf("The number is invalid\n");
return 1;
}
printf("You entered the number %ld\n", num);
if (garbage != NULL) {
printf("Additional garbage that was ignored is '%s'\n", garbage);
}
getch();
return 0;
}
This doesn't fix everything that is questionable about what you posted, but it should help you get off to a better start.
Output is:
tpost#tpost-desktop:~$ ./t
Enter the Number 1234abdc
You entered the number 1234
Additional garbage that was ignored is 'abdc'
Compiled via:
gcc -Wall -DLINUX_VERSION -o t t.c -lcurses
I'm not sure what platform you are using, so additional fixes to the code may be needed.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
One way is to use sscanf and check that there are no characters following the number. This is done most easily by adding a %c on the end and testing the return code, like this:
const char *yourString = ...;
int theValue, dummy;
if (sscanf(yourString, "%d%c", &theValue, &dummy) == 1) {
// Was a pure number, parsed into 'theValue'
} else {
// Either no number or had junk after it
}

Resources