I am trying to extract operators and operands from a string/char array, but unable to do so.
I've tried:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str[] = "222+333";
char *endPtr;
long int x = strtol(str,&endPtr,10);
printf("Number is %d \n" , x);
printf("Operator is %s \n" , *endPtr);
long int y = strtol(endPtr,&endPtr,10);
printf("Number is %d\n" , y);
return 0;}
Not sure how can I get the operator after using the strtol function.
Also are there any ways to do this without using libraries?
You almost had it, the issue with your code is the printing of the operator. %s is trying to print a null terminated string and requires a memory address. You are passing a character + which is probably ascii on your system so turns into the memory address 43
I've changed the printf statement to print a char vs a null terminated string and it works as you expect it too
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "222+333";
char *endPtr;
long int x = strtol(str, &endPtr, 10);
printf("Number is %d \n", x);
printf("Operator is %c \n", *endPtr);
long int y = strtol(endPtr, &endPtr, 10);
printf("Number is %d\n", y);
return 0;
}
To answer the comment question, endPtr is not pointing at the 3 it is pointing at the + which is valid for strtol to read ie a positive number, when you change the sign to divide or multiply it fails (should work with minus)
What you need to do is advance endPtr by 1 char past the operator that it is point at as in the code below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "222/333";
char *endPtr;
char theOperator= ' ';
long int x = strtol(str, &endPtr, 10);
printf("Number is %d \n", x);
theOperator= *endPtr;
++endPtr;
printf("Operator is %c \n", theOperator);
long int y = strtol(endPtr, &endPtr, 10);
printf("Number is %d\n", y);
return 0;
}
Related
I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.
Given task is:
Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)
So far I tried something like this, but for some reason it does not work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1];
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
}
for(i=0;i<10;i++){
if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
printf("%d. character '%c' is a digit.", i, character[i]);
}
else{
printf("%d. character '%c' is not a digit.", i, character[i]);
}
putchar('\n');
}
return 0;
}
Also I tried this, but it can not output correctly characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1], pom[10][1];
int num_character[10];
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
strcpy(pom[i],character[i]);
num_character[i]=atoi(character[i]);
}
for(i=0;i<10;i++){
if(num_character[i]!=0){
printf("Character '%c' is digit.", pom[i]);
}
else{
printf("Character '%c' is not digit.", pom[i]);
}
putchar('\n');
}
return 0;
}
isdigit() also does not work after I include ctype header.
You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.
The syntax for strcmp is:
int strcmp(const char *s1, const char *s2);
You need two strings as input in strcmp but you are using a string and a character.
Here, it is better to use a character array than a 2D array.
#include <stdio.h>
int main(void)
{
char character[10]; //a simple character array
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
scanf(" %c", &character[i]); //scan characters
}
for(i=0;i<10;i++)
{
if(character[i] >= zero && character[i] <= nine) //print
printf("%d. %c is a digit \n", i+1, character[i]);
else
printf("%d. %c is not a digit \n", i+1, character[i]);
}
}
I'm trying to assign values from user input stream to variables M and N. I can get my code to work if I specify M and N of type int. However, when i specify them as int16_t using the stdint.h it will read the first value in but the last value it won't. Why is this?
Here the code is working just fine...
#include <stdio.h>
#include <stdint.h>
int main(void)
{
char str[10];
int M, N;
fgets(str, 10, stdin);
sscanf(str, "%d%d", &M, &N);
printf("M is: %d\n", M);
printf("N is: %d\n", N);
return 0;
}
Here it does not work.
#include <stdio.h>
#include <stdint.h>
int main(void)
{
char str[10];
int16_t M, N;
fgets(str, 10, stdin);
sscanf(str, "%d%d", &M, &N);
printf("M is: %d\n", M);
printf("N is: %d\n", N);
return 0;
}
You're using the wrong specifier for the type int16_t, and thus the behavior is undefined.
The correct specifier for int16_t when used in scanf is SCNd16:
sscanf(str, "%"SCNd16" %"SCNd16, &M, &N);
And the specifier for printf is PRId16. Its usage is the same.
I'm trying to make a tiny program where you input for example 1 + 2 and the output should be the sum of those two numbers. But it keeps crashing and or won't do anything. What's going on?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *op;
char *first;
char *second;
printf("Enter operation\n");
scanf(" %s%s%s", &first, &op, &second);
int num1;
int num2;
int num3;
int add;
num1 = atoi(first);
num2 = atoi(op);
num3 = atoi(second);
add = num1 + num3;
printf("Sum = %i\n",add);
return 0;
}
atoi takes argument as const char * and not char . Your variables are of type char where as atoi converts string to int type.
Also you pass char * as argument to %d in scanf , that results in undefined behavour.
scanf(" %d%d%d", &first, &op, &second)
^^^^^^ expects int * not char *
i want to convert from binary to decimal but convert function does not return any thing where is the problem ??
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int convert(int x); //fun to convert from binary to decimal
int main()
{
int x; //binary number
printf("plz enter binary number !\n\nbinary: ");
scanf("%d",&x);
printf("\ndecimal: ",convert(x));
return 0;
}
int convert(int x)
{
int sum=0; //decimal number
int i=0;
int r; //remainder
while(x!=0){
r=x%10;
sum+=r*pow(2,i);
x=x/10;
i++;
}
return sum;
}
You can also try
scanf("%i", &x);
printf("%d", x);
you should change your printf in main().
printf("\ndecimal: %d",convert(x));// add %d
Because to print a value by printf need to spacify the data type such as for int %d for character %c.
binary numbers are series of ones and zeros (0|1), if you are referring to this type of numbers then check this source code below. you may also find this link useful http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int bintodec(char bin[32]){
int dec=0;
while(*bin){
if((*bin!='0') && *bin!='1')
break;// only accept '0' & '1'
dec<<=1;
dec+=*bin-'0';
bin++;
}
return dec;
}
int main(){
char bin[32]=""; //i believe binary numbers are series of '1' and '0'
printf("plz enter binary number !\n\nbinary: ");
scanf("%s",bin);
printf("\ndecimal: %d\n",bintodec(bin));
return 0;
}
Use format specifier:
%d for integer (in this case)
%c for character
%f for float value
%s for string
%lf for double
%x for hexadecimal
In this case change print statement to:
printf("\ndecimal: %d",convert(x));