Can someone give me the example of this algorithm [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
This algorithm written in Write great code Vol 1 book, is for conversion of string of decimal digits to an integer value:
Initialize a variable with zero; this will hold the final value.
If there are no more digits in the string, then the algorithm is complete,
and the variable holds the numeric value.
Fetch the next digit (going from left to right) from the string.
Multiply the variable by ten, and then add in the digit fetched in step 3.
Go to step 2 and repeat.
I don't know how the conversion takes place. Please give the example of this.

/* warning: naive and unsafe implement. don't use it for important projects */
int strtod(const char *str)
{
int ret = 0;
while (*str)
ret = ret * 10 + *str++ - '0';
return ret;
}

Consider the string "1134"
String Variable
()1134 0
(1)134 0 * 10 + 1 = 1
1(1)34 1 * 10 + 1 = 11
11(3)4 11 * 10 + 3 = 113
113(4) 113 * 10 + 4 = 1134

This is your standard string-to-int conversion algorithm:
char *s = "12345";
int res = 0; // 1. Initialize a variable with zero
while (*s) { // 2. If there are no more digits in the string...
int d = (*s++ - '0'); // 3. Fetch the next digit
res = 10*res + d; // 4. Multiply the variable by ten, and then...
} // 5. Go to step 2 and repeat.

Related

While loop increments in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to make my "Steps" or increments in a while loop such that my values of are like N=2, 4, 8, 16... basically powers of 2 till 2^20. I have tried to do
for(j=1;j<=20;j++){
m=pow(2,2*i);
MAX=pow(2,20);
INC=pow(2,i);
while(m<=MAX){
then have my code running inside this. But in the output it gives me 2,4,6,8,10,12.. does anyone know what the right way is to do this?
Thanks!
You can start with a value of 2 for m and multiply m by two every iteration. As an optimization you can replace the multiplication by two with an left shift:
int MAX = pow(2, 20);
int m = 2;
while(m <= MAX) {
//do your work
m <<= 1;
}
In each iteration of the while loop; multiply your variable by two.
int m = 2;
while(m < maxpow)
{
m = m * 2;
printf("%d\n", m);
}

How to convert a string to an integer in C programming language, based on this strict example? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Example:
// variable fixed
Char specialstring[PATH_MAX];
int integernumber;
int xx = 1 ;
int yy = 1 ;
strncopy( specialstring, "1369", ... bla);
// here we go !! help there below please
integernumber=atoi(specialstring);
mvprintw( yy , xx , "%d" , integernumber );
Please help me in the way to convert the specialstring to an integer?
thank you
In your code, you have two mistakes:
1) strncopy is not the function you wants its strncpy. its man page:
char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
Here s1 is destination string and s2 is source string , n is number of chars you wants to copy from source.
So correct:
strncopy( specialstring, "1369", ... bla);
^ ^ should be `n` num of chars you wants to
strncpy copy in `specialstring`
into
strncpy( specialstring, "1369", 4);
2) In declaration of specialstring, Char is wrong you should write small c
Char specialstring[PATH_MAX];
^ small letter
char specialstring[PATH_MAX];
3) atoi() is correct function you got to convert a string into int, if you wants to convert without atoi you can use sscanf() function like:
sscanf(specialstring,"%d", &integernumber);
View this: Working Code
You can use this to convert string to int without using atoi
int Convert(char * str)
{
int result =0;
int len=strlen(str);
for(int i=0,j=len-1;i<len;i++,j--)
{
result += ((int)str[i] - 48)*pow(10,j);
}
return result;
}

How to convert an integer to a character array using C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to convert an integer number to a character array in C.
Input:
int num = 221234;
The result is equivalent to:
char arr[6];
arr[0] = '2';
arr[1] = '2';
arr[2] = '1';
arr[3] = '2';
arr[4] = '3';
arr[5] = '4';
How can I do this?
Make use of the log10 function to determine the number of digits and do like below:
char * toArray(int number)
{
int n = log10(number) + 1;
int i;
char *numberArray = calloc(n, sizeof(char));
for (i = n-1; i >= 0; --i, number /= 10)
{
numberArray[i] = (number % 10) + '0';
}
return numberArray;
}
Or the other option is sprintf(yourCharArray,"%ld", intNumber);
'sprintf' will work fine, if your first argument is a pointer to a character (a pointer to a character is an array in 'c'), you'll have to make sure you have enough space for all the digits and a terminating '\0'.
For example, If an integer uses 32 bits, it has up to 10 decimal digits.
So your code should look like:
int i;
char s[11];
...
sprintf(s,"%ld", i);
The easy way is by using sprintf. I know others have suggested itoa, but a) it isn't part of the standard library, and b) sprintf gives you formatting options that itoa doesn't.
Use itoa, as is shown here.
char buf[5];
// Convert 123 to string [buf]
itoa(123, buf, 10);
buf will be a string array as you documented. You might need to increase the size of the buffer.
You may give a shot at using itoa. Another alternative is to use sprintf.

linux C string to int [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to convert this string "09195462965" to an int but I'm running into problems.
snippet of my code:
int n, p, pnum=0;
char buffer[256];
char *endptr;
long pnumber;
bzero(buffer,256);
p = read(sock,buffer,255);
pnumber = strtol(buffer, &endptr, pnum);
printf("n: %ld",pnumber);
p = write(sock,buffer,sizeof(buffer));
A client sends a string of "09195462965" then the server receives it.
Now on the server that string must be turned into an int i.e. 09195462965.
Note: the server sends the number as string.
You're using strtol() incorrectly, the last parameter should be the base that you want. For example if you want to store that number in base 10 (decimal):
long pnumber;
pnumber = strtol("09195462965", NULL, 10); //nst char *nptr is "09195462965"
//char **endptr is null
//int base is 10 for decimal
printf("n: %ld",pnumber);
>> 9195462965
Make sure you read the man page for the function you're using.
Passing pnum (which is set to 0) as your doing for the last parameter is causing it to spit back "0", because of the number you're passing in.
09195462965 has digits from 0-9 (so I assume you wanted dec) if you pass in "0" to strtol() then it's going to see that first 0 and will treat the number has octal, the problem with that is that octal numbers go from 0-7, thus the 9's are "out of bounds" for an octal number and as such strtol() spits back 0.
with a number like: 07175462765, you'd be fine to pass in pnum when it's 0.
That's too big to fit in an int. Try strtoumax and store it in an uintmax_t.
uintmax_t pnumber = strtoumax(buffer, &endptr, 10);
if (pnumber == UINTMAX_MAX && errno == ERANGE)
/* Too big. */
Alternatively, if you don't have strtoumax you can try strtoull et al.

How to convert from ASCII to Hex and vice versa? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to convert ASCII to HEX and HEX to ASCII by using a C program.
How can I do that?
Here's a simplistic function to convert one character to a hexadecimal string.
char hexDigit(unsigned n)
{
if (n < 10) {
return n + '0';
} else {
return (n - 10) + 'A';
}
}
void charToHex(char c, char hex[3])
{
hex[0] = hexDigit(c / 0x10);
hex[1] = hexDigit(c % 0x10);
hex[2] = '\0';
}
Its pretty easy. Scan through character by character ... best to start from the end. If the character is a number between 0 and 9 or a letter between a and f then place it in the correct position by left shifting it by the number of digits you've found so far.
For converting to a string then you do similar but first you mask and right shift the values. You then convert them to the character and place them in the string.

Resources