I'm trying to get a very large number (more than unsigned long long int). So I get it as a string and then convert it, digit by digit, into integer and use it.
#include <stdio.h>
#include <string.h>
int main()
{
char m[100];
int x;
scanf("%s",m);
for(int i=0; i<strlen(m); i++){
sscanf(m[i],"%d",&x);
printf("%d",x);
}
return 0;
}
However, during compiling it shows:
warning: passing argument 1 of ‘sscanf’ makes pointer from integer
without a cast
and
note: expected ‘const char * restrict’ but argument is of type ‘char’
And, when I run program it will give me the Segmentation fault (core dumped) error.
I also tried more simple code to find the problem:
#include <stdio.h>
#include <string.h>
int main()
{
char m[5];
char n;
int x;
scanf("%s",m);
n = m[1];
sscanf(n,"%d",&x);
return 0;
}
but nothing changed.
scanf doesn't apply to characters. Once you have the characters just convert digits to integers by subtracting '0' as a char:
for(int i=0; i<strlen(m); i++){
x = m[i] - '0'; // line to change
printf("%d",x);
}
Besides, just to make sure that the buffer won't overflow, 100 bytes is good, but you may want to use an according limit in scanf and check the return code:
if (scanf("%99s",m) == 1) {
Using sscanf to convert a single digit of a character string to an integer is the wrong approach. To do this, you only need to subtract the (integer) value of the representation of the character '0' from that digit. Like this:
#include <stdio.h>
#include <string.h>
int main()
{
char m[50]; // As pointed out, a 4-digit number isn't really very long, so let's make it bigger
int x;
scanf("%49s", m); // Limit the input to the length of the buffer!
for (size_t i = 0; i < strlen(m); i++) { // The "strlen" function return "size_t"
x = m[i] - '0'; // The characters `0` thru `9` are GUARANTEED to be sequential!
printf("%d", x);
}
return 0;
}
Related
I want to get the weight and the object in a list string( in this example I want to get the integer 501 and the string "kg bag of sugar". But I don't know-how may string after the integer. but I do know how many spaces before and after the integer (that is why I do +3 since there are 2 spaces before integer and 1 space at the end). I got a segmentation fault in my code.
Here is an example of what I am trying to do.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
/* get the weight and the object */
int main(void) {
char line[50] = " 501 kg bag of sugar"; //we don't know how many char after integer
char afterint[50];
long int weight, weight2;
int lenofint;
sscanf(line, "%ld", &weight);
weight2 = weight;
while (weight2 != 0) {
weight2 = weight2 / 10;
lenofint++;
}
afterint[0] = line[lenofint + 3]; // +3 since there are 2 spaces before integer and 1 space at the end
//printf("%c", afterint);
for (int j = 1; j < (strlen(line) - lenofint - 3); j++) {
afterint[j] = afterint[j] + line[j + lenofint + 3];
}
printf("%s", afterint);
}
Stop hard coding offsets and making this hard on yourself. The scanf family of functions includes an option, %n that will tell you how many characters have been processed in the scan up until that point. From there you can skip over whitespace and proceed to the remainder of your label.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char line[50] = " 501 kg bag of sugger";
long int weight;
int count;
if (sscanf(line, "%ld%n", &weight, &count) == 1)
{
char *suffix = line+count;
while (*suffix && isspace((unsigned char)*suffix))
++suffix;
puts(suffix);
}
}
Output
kg bag of sugger
As a bonus, by using this method you also get error checking. Note the check for return result from sscanf, which indicates the number of successful argument parses. If that isn't 1, it means whatever was in the buffer lead position could not be parsed successfully as %ld (long int), and therefore the rest is pointless.
You can use strtol() to read the number and get a pointer to the point in the string after the number. It would then point to kg bag of sugar. That way you don't need to do any back-calculations from the number. In any case, the number could have leading zeroes or such, so you can't know the length in characters from the numerical value anyway.
Then just skip over the whitespace from the pointer you got from strtol.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char *foo = " 501 kg bag of sugar";
char *thing = NULL;
int weight = 0;
weight = strtol(foo, &thing, 10);
while (isspace(*thing)) {
item++;
}
printf("weight: %d thing: %s\n", weight, thing);
}
Alternatively, I suppose you could do something like sscanf(foo, "%d %100c", &weight, buffer); to get both the number and the following string. (I'll leave it to you to pick a saner conversion than %100c.)
I'm new to C and am having a lot of issues with arrays that keeps coming up. I'm trying to write a method that takes in a string ("1234") and returns the odd digits, however it keeps printing 49 and I don't know why? Does it have something to do with how I'm assigning the arrays?
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int print_odd_digits(char number[100]) {
int size = sizeof(number[100]);
for (int i = 0; i < size; i++) {
if (number[i] % 2 == 1) {
printf("%d\n", number[i]);
}
}
return 0;
}
int main(void) {
print_odd_digits("1234");
}
sizeof(number[100]) is equivalent to sizeof(char), which is 1. What you want seems strlen(number).
Elements of number are not values of digits but character codes of digits. To convert number characters to values, you can subtract '0'. (character codes for 0 to 9 are defined to be continuous in C).
%d is for printing integer and 49 is ASCII code for the character 1. Use %c to print character corresponding to passed integer.
You may want newline only on end of the characters.
Try this:
#include <stdio.h>
#include <string.h> /* for using strlen() */
int print_odd_digits(char number[100]) {
int size = strlen(number);
for(int i = 0; i < size; i++){
if((number[i] - '0') % 2 == 1){
printf("%c",number[i]);
}
}
printf("\n");
return 0;
}
int main(void) {
print_odd_digits("1234");
}
You have a type error here. You are working with the ascii codes for the text characters for digits. You need to use the atoi(char* str)
function to convert the digits to a corresponding number and use math to determine a particular digit or loop over the characters and subtract -'0' the ascii code for the digit zero as digits are sequential.
#include <stdlib.h>
#include <stdio.h>
int main() {
char* digit_one_str = "1";
char digit_9 = '9';
printf("digit one ascii value: %d\n", digit_one_str[0]); // first char in string digit one
int number_one = atoi(digit_one_str);
printf("digit one parsed from sttring by atoi: %d\n", number_one);
printf("digit 9 as char converted with -'0' trick: %d", (int)(digit_9 - '0'));
}
Further it is better practice to take in a char* and a length than to work with fixed sized arrays or using strlen() where you don't actually have to. In C a string ends in a terminating '\0' character which might not even be present if the code calling your function is buggy or malicious.
I got a task from my college which is to find the length of a string using pointers in a function.Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define max 100
int lengthchar(char *array1[])
{
int a, x = 0;
for (a = 0; *(array1++) != '\0'; a++)
{
x++;
}
return x;
}
int main()
{
char arr1[max];
int length;
printf("Enter string\n");
gets(arr1);
length = lengthchar(arr1);
printf("Length=%d", length);
return 0;
}
But when I give input something bellow:
Enter string: av
Length=9
It shows the wrong length, what's my mistake?
This is because of the way to pass a string pointer as an argument to the function.
Just change char *array1[] to char *array1 or array1[].
Have a look at the implementation below:
int lengthchar(char array1[])
{
int a,x=0;
for(a=0;*(array1++)!='\0';a++)
{
x++;
}
return x;
}
PS: variable a can be removed by using a while loop.
In your function signature, you are telling the compiler that lenghtchar() expects a pointer to character strings, or **char in other words.
What you really want to do is to change your function from int lengthchar(char *array1[]) to int lengthchar(char array1[]) or int lengthchar(char *array1). This is a bit tricky since in C, you can address an array by using the address of its first element (aka, by using pointer to its first item).
Expert C Programming has a dedicated chapter on this topic.
Now, coming to your lengthchar() function, I would do some refactoring to eliminate the variable a and use a while loop instead. I have also included another alternative implementation that relies on pointer arithmetic (more fun to me :) )
Note also that I used fgets() instead of gets() which is considered deprecated since it does not do any bounds checking.
#include <stdio.h>
#include <stdlib.h>
#define max 100
/*
* returns the lenght of the string excluding the terminating
* NULL character
*/
int lengthchar(char *array1) {
int x = 0;
while (*array1++)
x++;
return x-1;
}
int lengthchar1(char *array1){
char *p;
for (p = array1; *p; p++)
;
return p - array1 - 1; /* -1 for \0 */
}
int main() {
char arr1[max];
int length;
printf("Enter string\n");
fgets(arr1, max, stdin);
length = lengthchar(arr1);
printf("Length=%d", length);
return 0;
}
On declaring or defining a function that shall take an array of type T as an argument, use the notation: T *array or T array[]; T may stand for any valid C data type such as char, int, float, double, etc...
As for your loop, the variable a seems to be redundant because it has no effect on any part of the program. The return value of the function lengthchar() is one more than the number of characters inputted.
Also you should avoid the function gets() because it is deemed dangerous and has been removed from the C standard; use fgets() instead.
Your code should look something like this:
#include <stdio.h>
#include <string.h>
#define max 100
int lengthchar(char *str)
{
int len = 0;
while (*str++)
{
len++;
}
return len;
}
int main()
{
char str1[max];
printf("Enter string:");
fgets(str1, max, stdin);
str1[strcspn(str1, "\n")] = 0; // remove new-line character
int length = lengthchar(str1);
printf("Length = %d\n", length);
return 0;
}
The program stops working.
Even if I put only one int.
I tried many different ways but can't figure out what is wrong.
I am trying to take input of integers separated by space.
There can be any no of integers.
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int i,j=0;
int b[100];
char a[100];
fgets(a,100,stdin);
for(i=0;i<strlen(a);i++)
{
b[i] = atoi(a[j]);
j=j+2;
}
for(i=0;i<strlen(b);i++)
{
printf("%d ",b[i]);
}
}
Here is the prototype of atoi you have to use a character array but you are sending a character only.atoi(str[i])
int atoi(const char *str)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
return(0);
}
Do the following:
for(i = 0; i < strlen(a); i += 2)
{
b[j] = a[i];
j++;
}
though atoi() accepts only a string argument or u can say constant char pointer and str[i] is nothing but a pointer pointing at a single character of the character array.
Therefore once we pass atoi(str[i]) , that means we are passing a character to the function, which will give an error.
Therefore you must pass the address of that particular character from where you want to convert the substring into a number i.e. atoi(&str[i]).
I have I problem. I get 2 warnings from console, but I dont know what's wrong with my code. Can you have look?
Program suppose to show lines with at least 11 characters and 4 numbers
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
if(isalpha(line)) numberAlpha++;
else if(isdigit(line)) numberDigit++;
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}
Both isalpha() and isdigit() takes an int, not a char *, as argument.
In your code, by passing the array name as the argument, you're essentially passing a char * (array name decays to the pointer to the first element when used as function argument), so, you're getting the warning.
You need to loop over the individual elements of line and pass them to the functions.
That said, just a suggestion, for hosted environment, int main() should be int main(void) to conform to the standard.
isalpha and isdigit are supposed to test if a char taken as int (a char can be safely converted to an int) is the encoding of an alphanumeric or digit character. You pass a char array, not an individual char. You need to test each char of the string you got, so you need a loop as:
for (int i=0; i<strlen(line); i++) {
if (isalpha(line[i])) numberAlpha++;
...
}
It is better to compute the length once:
int length = strlen(line);
for (int i=0; i<length; i++) {
...
}
You may also use a pointer to move along the string:
for (char *ptr = line; *ptr!=`\0`; ptr++) {
if (isalpha(*ptr)) ...
...
}
isalpha() and isdigit() functions take an int. But you are passing a char* i.e. the array line gets converted into a pointer to its first element (see: What is array decaying?). That's what the compiler complains about. You need to loop over line to find the number of digits and alphabets in it.
Also note that fgets() will read in the newline character if line has space. So, you need to trim it out before counting.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
line[strcspn(line, "\n")] = 0; // Remove the trailing newline, if any.
for (size_t i = 0; line[i]; i++) {
if(isalpha((unsigned char)line[i])) numberAlpha++;
else if((unsigned char)isdigit(line[i])) numberDigit++;
}
printf("alpha: %d, digits:%d \n", numberAlpha, numberDigit);
}
return 0;
}
Ok, i got something like this:
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
int i;
for(i=0; i<strlen(line); i++){
if(isalpha(line[i])) numberAlpha++;
else if(isdigit(line[i])) numberDigit++;
}
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}
Now the question is, if it is passible to make it first accepts data and then display only those line which follows the if statment. Now it shows line just after input it.