Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following scenario where any input string will be converted into an integer.
Example:
result = get_integer_from_string("100");
result == 100; // true
How can I write this function without using any libraries? I am able to do it by using libraries.
Follow these Steps:
Parse the input string.
Check if the character is a digit or not.
Use some logic to convert digit in character format to integer format.
Also, you can implement Exception in case, input is not integer string.
I cannot tell you the code, it would not help you in learning, try implementing code yourself, it is very easy!!
Loop over the string, from the end to the start. Get each digit, and convert it to a decimal value. Multiply the first (in the backward loop) by 1 and store the result. Multiply the second by 10 and add to the result of the previous. And so on.
This is very prone to error conditions, but should work if string is valid integer:
int str2int(const char* str) {
int result = 0;
char* p = str;
for (;;) {
char c = *p++;
if (c < '0' || c > '9')
break;
result *= 10;
result += c - '0';
}
return result;
}
It has behavior close to atoi() - stop processing on any non-digit, and return 0 for empty input.
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 2 years ago.
Improve this question
I want to create a function to take a string like this "2014-02-13T06:20:00"
and to convert to a long long int like 20140213062000.
Has anyone a idea how this can be done?
Here is an algorithm, you will just write the code:
define a long long variable n and initialize it to 0.
for each character c in the string:
if c is a digit, ie greater or equal to '0' and less or equal to '9':
multiply n by 10 and add the value represented by c, store the result in n.
else:
ignore the non digit character
n should have the expected value.
This would convert positive values. If the string has an initial - indicating a negative number, you would test that and negate the number at the end. Note also that overflowing the range of long long int has undefined behavior with this approach.
Your attempt in the comment needs some improvements:
long long string_To_long(const char sl[]) {
long long int n = 0;
for (int i = 0; sl[i] != '\0'; i++) {
char c = sl[i];
if (c >= '0' && c <= '9')
n = n * 10 + (c - '0');
}
return n;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to convert a char array to upper case in C. But it still prints out in lower case. The logic in conversion function looks okay to me. I don't know if the problem could be that the input I'm passing to this function is member of a structure variable.
This is the structure whose member I'm passing to conversion function:
typedef struct AESParams {
unsigned char output[192];
} AESParams;
My conversion fucntion is shown below:
void stringUpr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}
I call conversion function as follows:
AESParams parameters;
stringUpr(parameters.output);
If I print the output after calling "stringUpr" function, the value is still in lower case. Can anyone tell me what might be causing the issue here?
TIA...
Update: Code that writes value to output.
EVP_EncryptUpdate(&ctx, parameters->output, &outLen, input, length);
// This call happens in some other file. parameters is passed to the function that calls this function and after computation parameters is passed back to the place where I'm calling stringUpr().
I'm confident that this lines works and it gives the correct results after computation. It's just that I want to take this value and convert to upper case and write to a file.
Your program assumes that it's running using an ASCII character set, which is not guaranteed. Use the standard functions defined in ctype.h
void stringUpr(char* s)
{
int i = 0;
while(s[i] != '\0')
{
s[i++] = toupper((unsigned char)s[i]);
}
}
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 5 years ago.
Improve this question
Good night, what's the best way in C to count the number of possibilities of UNIQUE anagrams in a string with the maximum length of 256 without allows repetitions caused by same letters? The input would be just in uppercase and only alphabet letters are allowed, A-Z. I got stuck in the worst case of the program that got 26! a very large number that overflow even my double. I think I'm very lost here, I'm not so good in C. The program just need to shows the number of possibilities, not the anagram. Like:
LOL = 3
HOUSE = 120
OLD = 6
ABCDEFGHIJKLMNOPQRSTUVWXYZ = 403291461126605635584000000
Thank you guys very much... I tried a lot and failed in every single tried, I'm in distress with it. The way I got more closer of do it was in Pascal, but it also failed in some tests, and I can't use Pascal anyway. I'm using CodeBlocks on Windows that compiles with GCC.
You should calculate factorial of the length of the given string divided by the factorial of the occurrence of every letter.
long double logFactorial (int i) {
return i < 2 ? 0.L : (logFactorial (i-1)+log(long double (i));
}
int countLetter(const char* str, char c) {
int res = 0;
while (str && *str) {
res += *str++ == c;
}
return res;
}
long double numPermutations(const char* str) {
auto res = logFactorial (strlen(str));
for (char c = 'A'; c<='Z'; c++) {
res -= logFactorial (countLetter (str,c));
}
return exp((long double)res);
}
Pay attention!
There are several people here who were correct by saying that factorial of 26 cannot be stored even in 64bit integer.
Therefore, I changed my calculation to the logarithmic of the factorial number and store it in long double which I hope is precise enough (I think that the exp() function is not precise enough)
Nevertheless you cannot use this result as an integer value, unless you find a way to store it in 128bit integer or bigger...
You should test it too if this fits your problem.
There is a faster way to calculate the factorial for this problem by storing the results of 0! up to 26! in an array of the size of [27].
I will leave it to you for asking another question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
When I run this program it prints a strange character in the terminal. Can someone tell me what that is?
int main(){
char x=1;
printf("%c\n",x);
return 0;
}
This is because you are assigning 1 (ASCII value of a character) to x . Assign '1' to x. It will give output 1.
char x = '1';
printf("%c\n",x);
Its printing character 1 from the ASCII table
(making reasonable assumptions about your platform, that you aren't on an EBCDIC platform or something)
Characters in C should usually be assigned using characters, not by using ASCII encoding. For example:
char x = 'A';
printf("%c\n", x);
Will print the character 'A' on the terminal. By giving the character the ASCII index of 1, you're assigning it the START OF HEADING character (SOH). If you were looking for 'A' or '1', that would be:
char x = 65; // x = 'A'
char y = '1'; // y = '1'
But like I said, this is very awkward and requires ASCII memorization to read, so assigning numbers is very bad practice.
You can find an ASCII table at: http://www.asciitable.com/
It depends on what you want as output. If you think that your code is giving something weird as output, you certainly want '1' as output. And for that, you should replace your statement
char x = 1;
to
char x = '1';
Thats it! Your problem would be solved!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}