I'm new to C and need your help.
I'm trying to get an integer from a string that has a number in it.
I have with me a code I did but the only problem is when I'm assigning years as int i think I'm getting some sort of an address. Is there a way to get my char years into an in years?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sentence[]="trade_#_2009_#_invest_#_DEALING";
char *word=strtok(sentence, "_#_");
char *year=strtok(NULL,"_#_"); // assigning NULL for previousely where it left off
char *definition=strtok(NULL,"_#_");
char *synonyms=strtok(NULL,"_#_");
printf("%s\n", word);
printf("%s\n", year);
printf("%s\n", definition);
printf("%s\n", synonyms);
return 0;
}
int iYear = 0;
sscanf(year,"%d",&iYear);
This should be the simplest.
You might also want to look at atoi function.
Use strtol or strtoul.
Here's an example taken from the strtol page that I linked to above:
/* strtol example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,10);
li2 = strtol (pEnd,&pEnd,16);
li3 = strtol (pEnd,&pEnd,2);
li4 = strtol (pEnd,NULL,0);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return 0;
}
Related
I'm new to C (and programming) and it's possible that my question can be answered with some basic searching and reading; please point me to an answer to the question if it exists.
Let's say that I want to read a number that can possibly be above 999 from the user. From my experience, if I enter the value 10,000 (including the comma), the program would read the number until the comma and then stop taking input. Thus, the input would be 10 instead of 10000.
How can I make it read '10,000' as if it is 10000?
#include <stdio.h>
#include <stdlib.h>
void RemoveChar(char* Number, char chartoberemoved)
{
char *p, *s;
p = s = Number;
while (*s)//Run until last \r\n
{
if (*s != chartoberemoved)
{
*p++ = *s;
}
/* We always advance s. */
s++;
}
/* We 0-terminate p. */
*p = 0;
}
int main(int argc, char* argv[])
{
char array[32];
if (!argv[1])
return -1;
strcpy(array, argv[1]);
RemoveChar(array, ',');
int num = atoi(array);
printf("%d\n", num);
return 0;
}
Most implementation of scanf and printf on Linux support ' to tell that the number may have a thousands separator:
#include <stdio.h>
#include <locale.h>
int main(int argc, char const *argv[])
{
setlocale(LC_NUMERIC, ""); // set the locale so that "," is a thousands separator
const char* string = "10,000";
float num = 0;
sscanf(string, "%'f", &num);
printf("%f\n", num);
return 0;
}
returns
10000.000000
https://www.systutorials.com/docs/linux/man/3-printf/
use strtok its information and documentation can be found here. link
I want to parse an integer but my following code also accepts Strings like "3b" which start as a number but have appended chars. How do I reject such Strings?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
if(argc==2 && sscanf(argv[1], "%d", &n)==1 && n>0){
f(n);
return 0;
}
else{
exit(EXIT_FAILURE);
}
}
For your problem, you can use strtol() function from the #include <stdlib.h> library.
How to use strtol(Sample code from tutorial points)
#include <stdio.h>
#include <stdlib.h>
int main(){
char str[30] = "2030300 This is test";
char *ptr;
long ret;
ret = strtol(str, &ptr, 10);
printf("The number(unsigned long integer) is %ld\n", ret);
printf("String part is |%s|", ptr);
return(0);
}
Inside the strtol, it scans str, stores the words in a pointer, then the base of the number being converted. If the base is between 2 and 36, it is used as the radix of the number. But I recommend putting zero where the 10 is so it will automatically pick the right number. The rest is stored in ret.
The ctype.h library provides you the function isdigit(char) function that returns whether the char provided as argument is a digit or not.
You could iterate over argv[1] and check it this way.
I want to print a line similar as following:
====================================
And I need to control the count of the char, and able to specify which char to print.
I don't want to use loop.
Is it possible to do this with a single printf() statement?
#Update
I ask this because I use printf in this way sometimes:
printf("%10s\n", "abc");
So, if printf could do this, then it's possible to do what I ask, I am just not sure ... now I know it can't ...
Ok, I wrote a simple util function to do this:
#include <stdio.h>
void printRepeatChar(char c, int count) {
char cs[count+1];
int i;
for(i=0; i<count; i++)
cs[i] = c;
cs[count] = '\0';
printf("%s\n", cs);
}
int main(int argc, char * argv[]) {
printRepeatChar('-', 6*4);
}
maybe use memset() from string.h instead of the direct loop makes the code shorter, just as in the answers.
And, thank you all for help.
#include <stdio.h>
#include <string.h>
void PrintStuff( char to_print, int length ) {
// adjust buffer size as desired
char buffer[256];
// -1 for null terminator
if( length > sizeof(buffer)-1 ) length = sizeof(buffer)-1;
// fill buffer with desired character
memset( buffer, to_print, length );
// add null terminator
buffer[length] = 0;
// print to output
puts( buffer );
}
int main() {
PrintStuff( '=', 11 );
return 0;
}
http://ideone.com/RjPr83
And to answer the subquestion: no, printf cannot repeat a character as a formatting rule. It can only repeat spaces or 0's when padding.
#include <stdio.h>
#include <string.h>
int main(void) {
char c='=';
char a[20];
memset(a,c,(sizeof(a)-1));
a[19] = '\0';
printf("%s\n",a);
return 0;
}
Dynamic memory allocation and character scanning can be added to this .
Im looking for a 6 digit random number on the end of foo-, I have been trying for a few hours now with no success. only error messages
note: expected 'char *' but argument is of type 'int'
Ive tried to convert the int to char but it just doesn't like it, My code is below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char* concat(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
//in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
int main ()
{
srand(time(NULL));
int r = rand();
printf(concat("foo-", r));
return 0;
}
You could do this
printf("foo-%d", r);
or
char buffer[10];
sprintf(buffer, "%d", r);
char *c = concat("foo-", r);
printf("%s", c);
free(c);
This will use your function. Please read the manual pages for sprintf and printf
for example:
srand(time(NULL));
int r = rand();
char* str = (char*)malloc(sizeof(char)*20);
snprintf(str, 7, "%d", r);
if (strlen(str) < 6) { /* if r had less than 6 digits */
sprintf(str, "%06d", r);
}
char* s = concat("foo-", str);
printf("%s",s);
free(str);
free(s);
return 0;
from http://joequery.me/code/snprintf-c/ :
int snprintf(char *str, size_t size, const char *format, ...);
str is the buffer where printf output will be redirected to. size is the maximum number of bytes(characters) that will be written to the
buffer, including the terminating null character that snprintf
automatically places for you. The format and the optional ...
arguments are just the string formats like "%d", myint as seen in
printf.
so, in order to get a 6 digit number converted, you specify in snprintf() the size argument to 7 (you include a null character).
sprintf() function sends formatted output to a string pointed to by the first argument (in our case this is str). %06d format provides that to str at least 6 digits will be sent.
CONCLUSION
you want to convert a 6 digit number to a char array. if the number had at least 6 digits, with snprintf() you will get the first 6 digits of the number that was converted. if it has less than 6 digits, the sprintf() will add zeroes at the beginning until there are 6 digits. so, if r = 101; the result would be 000101.
note that with your usage of concat function:
printf(concat("foo-",r));
compiler warns about (gcc in my case):
warning: format not a string literal and no format arguments
[-Wformat-security]
printf() function needs to know the format of the string argument that is passed. there's a nice answer about it right here : warning: format not a string literal and no format arguments
In the example I'm using sprintf to convert int to char[]. Also I'm using modulo operation to ensure the result will have no more than 6 digits. The %06d argument for sprintf will ensure that the result will have no less than 6 digits.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char buf[7] = { 0 };
int r;
srand(time(NULL));
r = rand() % 1000000;
sprintf(buf, "%06d", r);
printf("foo-%s\n", buf);
return 0;
}
Your problem occurs when you attempt to call concat with the int value r as the second argument. Notice your definition of concat:
char* concat(char *s1, char *s2);
The second argument is char *s2. In your program you attempt to call it with:
int r = rand();
printf(concat("foo-", r));
r is an integer. Try this to correct the problem:
int main ()
{
srand(time(NULL));
int r = rand();
char str[] = "bar";
printf(concat("foo-", str));
return 0;
}
Is there any dedicated function for converting the binary values to decimal values.
such as (1111 to 15 ) , ( 0011 to 3 ) .
Thanks in Advance
Yes, the strtol function has a base parameter you can use for this purpose.
Here's an example with some basic error handling:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* input = "11001";
char* endptr;
int val = strtol(input, &endptr, 2);
if (*endptr == '\0')
{
printf("Got only the integer: %d\n", val);
}
else
{
printf("Got an integer %d\n", val);
printf("Leftover: %s\n", endptr);
}
return 0;
}
This correctly parses and prints the integer 25 (which is 11001 in binary). The error handling of strtol allows noticing when parts of the string can't be parsed as an integer in the desired base. You'd want to learn more about this by reading in the reference I've linked to above.
Parse it with strtol, then convert to a string with one of the printf functions. E.g.
char binary[] = "111";
long l = strtol(binary, 0, 2);
char *s = malloc(sizeof binary);
sprintf(s, "%ld\n", l);
This allocates more space than needed.