when i execute that with 'é' it is accepted although the test! help!!
#include <stdio.h>
#include <string.h>
int main ()
{
char ch[10];
int i,k,k1;
do
{
k=0; i=0;
printf("Write a sentence without accentuated letters:\n");
scanf("%s",ch);
k1=strlen(ch);
while ((k==0)&&(i<k1))
{
if (ch[i]=='é') k=1;
i++;
}
}
while (k==1);
return 0;
}
The problem is probably with encoding. é can have different numerical representation depending on the encoding standard used. If your source code editor, compiler and your command line use different encodings, things will never work this way. You might want to switch to UTF-8.
Related
I need to code a program that takes input from a given register (sending bytes) to a printer and the first sentence has to be the student name in uppercase which I tried with toupper() by converting the register to a given variable (char ch;), though the given result was not met.
The next one is for the faculty number (it says in "condensed" font? Not sure what that is supposed to mean, please correct me if I'm wrong but I take it as it's supposed to be all lowercase?)
Finally, the last given sentence should print the date on the screen as well but unfortunately it doesn't say whether it should take the system date or whatever. (I've done it using printf and written the current date)
Here is the code I've written so far:
(the toupper() function does not work)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <bios.h>
#include <string.h>
#include <ctype.h>
void main()
{
int i; char ch; char row[1000];
union REGS r;
clrscr();
memset(row,0,sizeof(row));
r.h.ah=0; // function 0h
int86(0x16,&r,&r);
for(i=0; i<1;i++)
{
ch = row[i];
printf("student name", toupper(ch));
ch = r.h.al;
r.h.ah=0;
int86(0x17,&r,&r);
r.x.dx=0;
printf("\n");
printf("16630960",row[i]);
row[i]=r.h.al;
r.h.ah=0;
int86(0x17,&r,&r);
r.x.dx=0;
printf("\n");
printf("10-04-2022",row[i]);
row[i]=r.h.al;
r.h.ah=0;
int86(0x17,&r,&r);
r.x.dx=0;
}
r.h.ah=0;
r.h.al=0x0A;
r.x.dx=0;
int86(0x17,&r,&r);
getch();
delay(100);
}
I am not sure what all that DOS interrupt thing is doing but I can at least fix the 'uppercase is broken' complaint
printf("student name", toupper(ch));
You are attempting to print one character of the name (thats probably not correct, but lets at least get that one character printed)
printf("student name %c", toupper(ch));
Now you need to fix all the other printfs
to convert whole string to upper case, assuming 'row' contains the string
int len = strlen(row);
for(int i = 0; i < len; i++){
row[i] = toupper(row[i]);
}
i am a starter and i have written a code:
#include<stdio.h>
char c;
char a[100]={0};
int i;
int main(void)
{
while(1)
{
i=0;
while(1){
if((c=getchar())!='\n'){
a[i]=c;
i++;
} else {
break;
}
}
printf("%s\n",a);
}
return 0;
}
and now i have idea that i want to add a break statement that when i type q, the program will break. so my new program is:
#include<stdio.h>
char c;
char a[100]={0};
int i;
int main(void)
{
while(1)
{
i=0;
while(1){
if((c=getchar())!='\n'){
a[i]=c;
i++;
} else {
break;
}
}
printf("%s\n",a);
if(getchar()=='q')break; /*i added this*/
}
return 0;
}
i know because the getchar() conflict the code is not correct, how can i do, to add the type q break command. thanks a lot.
i am a starter and i have written a code
Uh oh! Here goes... Firstly, the odds of successfully learning the portable, standard-compliant C programming language are overwhelmingly low for anyone who tries to learn by unguided trial and error. There's a really nice book you can use as a guide, but you need to read it AND do the exercises as you stumble across them. That's K&R2E, if you're interested.
Anything resembling char c; c = getchar(); is erroneous because the narrowing conversion from int (which getchar returns) to char discards error handling information.
Any character values returned by getchar will be as unsigned char values converted to an int, so it'd technically be more correct (though still incorrect) to use that type.
Error values are returned as a negative integer such as EOF, not to be confused with character values, which is exactly what your code does wrong by discarding the error information.
getchar returns int, so store the value into an int, then test for errors, then, if you must, convert it down to an unsigned char.
and now i have idea that i want to add a break statement that when i type q, the program will break.
Following on from the previous discussion, such a requirement is superfluous, nonetheless easy to incorporate if you're handling errors correctly. Just insert an extra test to match against q into the error handling, and presto!
Nonetheless, the error handling most likely already solves this problem, as most OSes have a mechanism to close stdin, which would cause getchar to return EOF, thus triggering the error handling clause. This is typically achieved by pressing CTRL+d in Linux, or CTRL+z in Windows, for example.
In conclusion, providing you aren't already reading one, it seems a nice book about C isn't all that'd benefit you; a nice book about console scripting for your favourite Unix-like OS would also be highly beneficial.
Unlikely to be what you actually want, but at least this will work
#include<stdio.h>
int main(void)
{
char a[100]={0};
int i = 0;
do {
int c = getchar();
switch (c) {
case EOF:
/*EOF*/
return -1;
case '\n':
a[i] = 0;
printf("%s\n",a);
i= 0;
break;
default:
a[i++] =c;
break;
} while (c != 'q' || i > 1);
return 0;
}
Try this code:
#include <stdio.h>
#include <stdlib.h>
#define q 113 --> These values are coming from ASCII table.
#define Q 81
int main(void)
{
char ch = 0;
do
{
ch = getchar();
switch(ch)
{
case q:
// your logic goes here
exit(0);
case Q:
// your logic goes here
exit(0);
/* more cases can come here */
}
}
while(1);
}
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<string.h>
#define number_of_letters 26
bool IsPangram(char* string);
int main(){
char check[100];
when i put the output as "the quick brown fox jumps over the lazy dog" the output is no a pangram
and when i put a to z values on one single line is gives the correct output
scanf("%s",&check);
if(IsPangram(check)){
printf("the string entered is pangram");
}
else{
printf("not a pangram");
}
return 0;
}
there is the function for pangram
bool IsPangram(char* string){
bool flags[number_of_letters];
int size=strlen(string);
bool ispangram=true;
int i;
char c;
// for all the alfabets to be setting them to false
for(i=0;i<number_of_letters;i++){
flags[i]=false;
}
// for converting the uppper case letter to the small one
for(i=0;i<size;i++){
c=tolower(string[i]);
if(islower(c)){
flags[string[i]-'a']=true;
}
}
// for checking the the lettters to no pangram
for(i=0;(i<number_of_letters && ispangram==true);i++){
if(flags[i]==false){
ispangram=false;
}
}
return ispangram;
}
You don't need use & when passing a string as a char *, since arrays decay to pointers when passed as parameters.
So:
scanf("%s",&check);
should be:
scanf("%s", check);
And some general advice: turn on compiler warnings to help catch simple mistakes such as this, and learn basic debugging techniques (stepping through code in your debugger, adding strategic printf statements, etc).
scanf() with the %s format specifier will stop at whitespace. Try fgets(check,100,stdin) instead... that will read (up to) a full line, and limit the number of characters to 99 + nul so you won't exceed the size of check. It may leave a newline as the last character, but your algorithm would ignore that anyway.
Additionally, in IsPangram(), c should be an int instead of a char (to match tolower() etc.), and change this:
// for converting the uppper case letter to the small one
for(i=0;i<size;i++){
c=tolower(string[i]);
if(islower(c)){
flags[string[i]-'a']=true;
}
}
...to this:
// for converting the uppper case letter to the small one
for(i=0;i<size;i++){
c=tolower(string[i]); /* <== c will always be lowercase */
if(isalpha(c)){ /* <== Check that c is a letter */
flags[c-'a']=true; /* <== use c, as string[i] may be uppercase */
}
}
...for the reasons indicated in the added comments.
scanf cannot get strings with spaces. So use fgets
fgets(check,sizeof(check),stdin);
Or use
scanf("%[^\n]s",check);
This reads a string till a newline character is encountered.
The default scanf stops reading when a space is encountered.
Here you go:
#include <ctype.h>
void uc(char*s){for(;*s;*s=toupper(*s),s++);}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char*argv[])
{int i,j,n=0,c[256]={0};
char*s,*w[1024]={NULL};
do w[n]=malloc(1024),fgets(w[n],1024,stdin),w[n][strlen(w[n])-1]='\0',uc(w[n]),n++;
while(n<1024&&strlen(w[n-1]));
for(--n,i=0;i<n;++i)
for(j=0;j<strlen(w[i]);++j)
c[w[i][j]]=1;
for(j=0,i='A';i<='Z';++i)j+=c[i];
printf("It's %sa pangram\n",j==26?"":"not ");
for(i=0;i<=n;free(w[i++]));}
So I am a very beginner to C programming (I have used Ruby, Python and Haskell before) and I am having trouble getting the most simple thing to work in C (probably because of all the manual memory stuff). Anyway, what I am trying to do is (using simple constructs) make a script that just echoes what the user inputs to the console.
e.g. user inputs hi, console prints hi.
This is what I came up with.
Also, I haven't really mastered pointers, so none of that.
// echo C script
int echo();
int main() {
echo();
return 0;
}
int echo() {
char input[500];
while (1) {
if (scanf("%[^\n]", input) > 0) {
printf("%s\n", input);
}
input[0] = 0;
}
return 1;
}
I realize that there is a bunch of bad practices here, like setting a giant string array, but that is just for simplifying it.
Anyway, my problem is that it repeats the first input then the input freezes. As far as I can tell, it freezes during the while loop (1 is never returned).
Any help would be appreciated.
Oh, and using TCC as the compiler.
You don't need an array for echo
#include <stdio.h>
int main(void)
{
int c;
while((c = getchar()) != EOF) putchar(c);
return 0;
}
It's fine that you have such a large string allocated, as long as it's possible for users to input a string of that length. What I would use for input is fgets (read this for more information). Proper usage in your situation, given that you still would like to use the string of size 500, would be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int echo(){
char input[500];
while(fgets(input, 500, STDIN)){ //read from STDIN (aka command-line)
printf("%s\n", input); //print out what user typed in
memset(input, 0, strlen(input)); //reset string to all 0's
}
return 1;
}
Note that changing the value of 500 to whatever smaller number (I would normally go with some power of 2 by convention, like 512, but it doesn't really matter) will limit the length of the user's input to that number. Also note that I didn't test my code but it should work.
scanf("%[^\n]", input
Should be:
scanf("%s",input)
Then after your if you should do:
memset(input,0,500);
There are many ways of accomplishing this task however the easiest would be to read from stdin one byte at a time and output that byte to stdout as you process each byte.
Snippet:
#include <stdio.h>
int main( void ) {
// Iterates until EOF is sent.
for ( int byte = getchar(); byte != EOF; byte = getchar() ) {
// Outputs to stdout the byte.
putchar( byte );
}
return 0;
}
Remark:
You must store the byte that you are reading through stdin in an integer. This is because you are not guaranteed that char is signed or unsigned, there are in fact 3 char types in C (char, signed char and unsigned char). Include the limits library to determine whether a char is signed or not in your environment.
You must compile using the C99 standards, otherwise move the declaration of byte outside of the for loop.
Its been a while now and im still trying to get a certain code to work. I asked some question about different commands etc. before, but now I hope this is the final one (combining all questions in one code).
I basically want to :
*Scan an input (should be character ? )
*Check if its a number
*If not, return error
*Convert that character into a float number
*Copy the value to another variable ( I called it imp here)
Here is what I came up with :
EDITED CODE*
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){
int digits;
float imp=0;
char* alpha;
do{
printf("Enter input\n\n");
scanf("\n%c",alpha);
digits=isdigit(alpha);
if(digits==0){
printf("error\n\n");
}
imp=atof(alpha);
}while(digits==0);
}
The problem is this code does not work at all ... It gives me that atof must be of a const char and whenever I try changing it around, it just keeps failing. I am frustrated and forced to ask here, because I believe I have tried alot and I keep failing, but I wont be relieved until I get it to work xD So I really need your help guys.
Please tell me why isnt this code working, what am I doing wrong ? I am still learning C and really appreciate your help :)
EDIT
Error given atm is :
Argument no 1 of 'isdigit' must be of type 'const int', not '<ptr>char'
EDIT
This code compiles fine, but crashes when an input is entered.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){
int digits;
float imp=0;
char* alpha=0;
do{
printf("Enter input\n\n");
scanf("\n%s",alpha);
digits=(isdigit(alpha[0]));
imp=atof(alpha);
}while(digits==0);
}
Why not have scanf do the atof conversion for you?
#include <stdio.h>
int main()
{
float imp=0;
while (1)
{
printf("Enter input\n\n");
if (scanf("%f", &imp) < 1) break;
}
return 0;
}
Your most recent example is failing because alpha is a NULL pointer. Declare it as char alpha[40]; to allocate space for it. You'll probably want to use %40s in your format string to prevent scanf from overflowing alpha.
Also, use strtod instead of atof and you'll know whether the conversion was successful (better than your method of using isdigit which will fail on a negative integer).
You probably need to use %s instead of %c and to put it in char array (char*). You also probably get an error that you need to use const char* and not const char.
You don't want to read just one character - you want to read an entire string...
EDIT:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(){
int digits,i;
float imp=0;
char* alpha = malloc(100); /* 100 is for example */
do{
printf("Enter input\n\n");
scanf("\n%s",&alpha);
for (i = 0; i != 100; ++i)
{
if (alpha[i] == '\0')
break;
if (!isdigit(alpha[i]))
{
printf("error\n\n");
return ...;
}
}
imp=atof(alpha);
}while(true);
}