Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having the issues of trying to separate 1s or 2d into int 1 and char 's' and then storing them as separate values. How would I go about doing this. I know I can't do it with strtok since there are no delimiters.
Use strtol. For example,
char *endptr;
val = strtol(input_str, &endptr, 10;
next_char = *endptr;
As discussed in the manpage, http://man7.org/linux/man-pages/man3/strtol.3.html, the second parameter is a pointer to a char pointer and after the conversion, the char pointer points to the following character.
You can access each char from the string by index. To convert char to int (not by ascii value, '1' to 1) you just do the following:
int a = c[0]-'0';
and for the char:
char b = c[1];
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm a new c programmer.
I was asked to get an ID number as an input and if the ID length < 9 I need to put '0' before the number.
example:
input: 12345
output: 000012345
I can use the libraries: stdio, stdlib, string, math and time.
You can use printf for this:
#include <stdio.h>
int main () {
printf("%09d\n", 12345);
return(0);
}
Documentation
There is an easy solution in here.
But if you want a hard solution, where you juggle around with strings and such, you can do the following:
convert your id into a string (if it isn't already one, but argv should a string by default)
calculate the number of zeros you need, by doing 9-idString Length
create a "buffer" string variable
for loop from 0 to 9-idString and use strcat to add "0"s to your buffer string.
add your id to your buffer string using strcat
Example code would be something like this:
int id = 12345;
char idstring[10];
char buffer[10];
sprintf(idstring, "%d", id);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
argv variant:
char idstring[10];
char buffer[10];
sprintf(idstring, "%s", argv[1]);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Was talking to a colleague today on one-spot errors - I.e. errors (or at least patterns that should ring an alarm bell) in code that a decent programmer should be able to spot at a single glance like
x = malloc (strlen(y));
while (!feof (f)) {
...
}
char *f(){
char x[100];
...
return x;
}
Who has similar snippets of such patterns? I would suggest anyone who has been on SO for a while will have his personal favourites of those
char *buf;
scanf("%s", buf);
This is wrong, because no memory has been allocated for buf.
char buf[100];
scanf("%s", &buf);
This is wrong, because scanf expects a char *, not a char (*)[n].
char c;
while ((c = getchar()) != EOF)
putchar(c);
This is wrong, because EOF does not fit in the range of a char. Use int instead.
fflush(stdin);
fflush is undefined for input streams, like stdin, albeit this is implemented as an extension in some compilers, like Microsoft C.
#define IN 0;
Do not put semicolons at the end of a #define.
blk = realloc(blk, n);
If realloc fails, any contents in blk will be lost, because realloc will return NULL. To solve the problem, copy the return value into a temporary and only if the temporary is not NULL, copy to the final destination.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
int main(int argc, char *argv[])
{
int param = *argv[1];
printf("Parameter = %d\n",param);
}
I run it ./myProgram 5 and I expect 5 as an output. What I get is 53 for some reason.
As you can see from the declaration, argv is an array of char pointers. This means that your main function is not being passed the numerical value 5, but the (probably ASCII- or UTF8-) encoded string value "5". If you look at ASCII table, you will see that the character "5" is encoded as the numerical value 53 in ASCII. You can also see that the letter "a" is encoded as number 97, so running ./myProgram a should output Parameter = 97.
To get the numerical value of an ASCII-encoded string you can use atoi(), e.g. write
int main(int argc, char *argv[]) {
int param = atoi(argv[1]);
printf("Parameter = %d\n",param);
}
C does not implcitily convert an array's content to any type.
To try to convert a 0-terminated char-array (a C "string") to an int consider using strtol().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to print in a file a string with a fixed size. Something like this:
#define SIZE 30
main()
{
FILE *fp = fopen("myfile.txt","w+");
char s[10];
sprintf(s, "my text");
fprintf(fp, "%SIZEs", s);
fclose(fp);
}
but I keep getting errors.. help?
You should define your format string like the following:
fprintf(fp, "%*s", SIZE, s); // Right aligned string
fprintf(fp, "%-*s", SIZE, s); // Left aligned string
From the printf man page:
The precision
Instead of a decimal digit string one may write "*" to specify that the precision is given in the next argument
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Input: AB5, BC2, CD3, BE4
When we input i/p to a program as above. we need to get each string separated by "," and also need to separate string to char by char like getting 'A' 'B' '5' .. how to do this efficiently in c???
I have implemented like storing whole string in an character array and then for loop processing each index of char array to get the char by char by that string..
char a[1000] = "AB5,BC2,CD3";
len = strlen(a);
for (int i=0;i<len;i++)
printf("%c",a[i]);
But is there any other efficient way of doing the above?
int c;
while ((c = getchar()) != EOF) {
if (c == ',')
putchar('\n');
else if (isalnum(c))
putchar(c);
}
does the same as your program, but without the array, translating , to newline and swallowing whitespace.
I'd do this in two passes. First, I'd use strtok() to separate the data on the commas (delimiter characters ',' and ' ').
Once you've done that, you can simple separate each character of each token.