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 5 years ago.
Improve this question
I have no problem in generate random numbers with rand()
I have to create an output file with a string of char e.g. "AQSDJEIOFHDUK"
When the result of rand ()%26 +1 is 1 I've to print in the file "A" when the result is "2" B and so on. I already know a priori how many char will be in the string let's say 50.
I've to do that in C language.
Which function I should use?
Strcat?
Simple set an array with random letters selected via rand()
#define RANDOM_STRING_LENGTH 50
char buf[RANDOM_STRING_LENGTH + 1];
for (size_t i = 0; i < RANDOM_STRING_LENGTH; i++) {
buf[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() %26];
}
buf[RANDOM_STRING_LENGTH] = '\0';
puts(buf);
You can use the ASCII table of characters to generate valid characters. The base for the uppercase alphabet is at 65, so when you generate
int rnd = rand() % 26;
you can say something like
char new_char = 65 + rnd;
or as others have stated, it is easier to read
char new_char = 'A' + rnd;
which should give you one character in the ascii table of characters based off of the result of rand() % 26. Remember, in C an unsigned char is really just a data type that can range from the number 0, to the number 255.
(as stated below, a signed char can range from -126 to 127)
Things like 'a' and 'b' are translated into their ascii equivalent. (i.e. the corresponding number between 0 and 255 that they represent.)
(unless you are using UNICODE of course.)
I hope this helped.
Related
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 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to parse a file that has following data eg:
MAGICNUMBER 400
4 is = 0x34
0 is = 0x30
4
0
0
are different unsigned chars
what i want is those different chars to be converted into
unsigned int x = 400;
when parsing them into my program i want to merge them into one integer i tried bitshifting but it didn't work and i probably did it very wrong and got a very large number probably due misunderstanding of something, what i'm susposed to do to merge those numbers without string tricks and without using std but only using bitshift with a explanation how it works?
Each digit is c - '0'. When you get a new digit, you know that prior ones are one decimal place greater, so you multiply the current number by 10 and add the new digit:
char *s = "400";
int sum = 0;
while(*s >= '0' && *s <= '9') {
sum = 10 * sum + (*s - '0');
s++;
}
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 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 4 years ago.
Improve this question
In the code below, if I enter 11(1 and 1), the output is:[]1. [] part is not garbage, I just used the symbols("[]") to express the empty output. Therefore the real output is just 1 with one empty space if front of 1.
int main(void)
{
int x = 1;
char y = 1;
x = getchar();
y = getchar();
x = x - x;
putchar(x);
putchar(y);
}
In addition, if I replace x = x - x; to
x -= 3;
and enter 11 or 22, it gives me output like *1 or /2.
Why is this happening?
Thank you.
You have to think in terms of ASCII values. When you perform x = x - x, you're saying x = 0 (the ascii code for whatever character I type in for x minus itself is always 0)
Then look up 0 in the ASCII table, and you'll see null. So it will print null (looks like a space) and a 1.
When you perform x -= 3;, you're taking the numeric ascii code for the character you typed in, and subtracting 3. If you look at the ASCII table, you'll notice that 3 characters before the character 1 is * and three characters before 2 is /. This explains the results you are getting.
If you intend to convert the characters into the numeric values it represents, there is a bunch of ways to do this, you can subtract '0' or use the atoi function after converting the char to a string in C.
-'0' method
int numeric = x - '0';
atoi method requires a conversion to string.
char str[2] = "\0";
str[0] = x;
int numeric = atoi(str);
Both these will not make sense if you typed in a non-numeric character, e.g. aa
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
I was wondering if it is possible to index an array by using a binary number instead of a decimal number. For instance, arr[binary].
Yep that's definitely possible. Just prepend your binary number with 0b
int array[] = {1,2,4,6};
printf("%d\n", array[0b0001]); // prints 2
from https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html binary constants can be written using the 0b syntax
As pointed out, all numbers stored on a computer are binary. Binary is the only thing that can be stored on a computer.
And, C does not support binary syntax. (Or perhaps come C compilers do?)
You could however convert a string from binary like this:
var value = arr[BinaryToInt("1011")];
int BinaryToInt(string s)
{
int value = 0;
int bitValue = 1;
for (int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1')
value += bitValue;
bitValue <<= 1;
}
return value;
}
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
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.