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
Can anyone give me a complete example of how to generate an Alphanumeric String randomly
like (ARG534UJ6) using C ? I'm completely new to C.
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
This is not working.
As pointed out by #IngoLeonhardt, use % (sizeof(alphanum) - 1) instead of % sizeof(alphanum)
My guess is that you don't have room for your string, try:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
int main(void)
{
char *str = malloc(8 + 1);
/* initialize random seed: */
srand(time(NULL));
gen_random(str, 8);
printf("%s\n", str);
free(str);
return 0;
}
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 6 years ago.
Improve this question
TASK: // target - starting an argument in which to record results
target should be a "source" left supplemented with "fill_char" to length "cnt". If cnt is less than the length of the source, it becomes equal to the target source.
// Now is moving on right and must to change something but I'm not sure what and how. Can you give me advice. And if you have some different decision please share with me. Thank you :)))!
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <batch_util.h>
int lpad(char* target, char* source, char fill_char, int cnt)
{
int i;
int source_size = strlen(source);
for (i = 0; i < source_size; i++)
target[i] = source[i];
for (i = source_size; i < cnt; i++)
target[i] = fill_char;
target[i] = '\0';
return 0;
}
int main(int argc, char* argv[])
{
char source[128];
char target[128];
char fill_char;
int cnt;
scanf("%s\n", &source);
scanf("%c\n", &fill_char);
scanf("%d\n", &cnt);
lpad(target, source, fill_char, cnt);
printf("%s\n", target);
return 0;
}
For starters you can use standard C string functions instead of loops.
It seems you mean the following
#include <stdio.h>
#include <string.h>
char * lpad( char *target, const char *source, char fill_char, size_t cnt )
{
size_t source_size = strlen( source );
if ( source_size < cnt )
{
memset( target, fill_char, cnt - source_size );
}
strcpy( target + ( source_size < cnt ? cnt - source_size : 0 ), source );
return target;
}
#define N 16
int main(void)
{
char *source= "monkey";
char target[N];
puts( lpad( target, source, '*', N - 1 ) );
return 0;
}
The program output is
*********monkey
As for your code then the function does not append anything to the left.
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
How do you split a string:
char *mystring = "12345"
into an integer array which looks like this:
[1, 2, 3, 4, 5]
I have tried something like the code below, but I'm not entirely sure if it's reliable, and I think it will be easy to break. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char *mystring = "12345";
int string_size, i, length;
string_size = strlen(mystring);
int values[string_size];
for (i = 0; mystring[i] != '\0'; i++) {
values[i] = mystring[i] - 48;
}
length = sizeof(values)/sizeof(*values);
for (i = 0; i < length; i++) {
printf("%d ", values[i]);
}
return 0;
}
Which outputs:
1 2 3 4 5
Is there a more C like way I can do this?
The odd thing I see, which isn't itself a problem, is that you calculate the length of the string/array three different ways:
string_size = strlen(mystring);
for (i = 0; mystring[i] != '\0'; i++) {
length = sizeof(values)/sizeof(*values);
where just one method is sufficient:
#include <stdio.h>
#include <string.h>
int main(void) {
char *mystring = "12345";
size_t length = strlen(mystring);
int values[length];
for (int i = 0; i < length; i++) {
values[i] = mystring[i] - '0';
}
for (int i = 0; i < length; i++) {
printf("%d ", values[i]);
}
printf("\n");
return 0;
}
You can replace 48 with '0' for readability.
You can change all loops to loop until string_size like the first one, no need to change the method for each loop.
And finally if you're going to return that array anywhere outside of local function, you should probably malloc() it rather than use a local/stack variable.
But otherwise, it's pretty simple and it works.
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 6 years ago.
Improve this question
I have str:
char *str = "lala";
Now, I would like convert any chars in str to hexadecimal, example:
str = convert(str);
print str: 0x6C 0x61 0x6C 0x61
^ l ^ a ^ l ^ a
How I can do that ?
char *convert(char const *str) {
int len = strlen(str);
char *retVal = (char *)malloc(5 * len);
char *pos = retVal;
int i;
for(i = 0; i < len; ++i, pos += 5) sprintf(pos, i? " 0x%x" : "0x%x", str[i]);
retVal[5 * len - 1] = '\0';
return retVal;
}
Might have missed something, haven't used C for eight years.
Simply by asking printf to do it :
void convert(char* str, size_t length) {
size_t i;
for(i = 0; i < length; i++)
printf("0x%02x ", str[i]);
}
You can achieve it by using this implementation....
#define MAX 100
char *convert(char *str)
{
char *hexStr = (char *)malloc(strnlen(str, MAX) * 5);
if (hexStr == NULL)
return NULL;
int i,j;
for (i=0, j=0; str[i]; j+=5, i++)
sprintf(hexStr + j, "0x%02x ", str[i]);
hexStr[--j] = '\0';
return hexStr;
}
char* x = str - 1;
while(*++x) printf("%02x ", (int) *x); // Print one character in hex
printf("\n") // Finish with a carriage return
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 7 years ago.
Improve this question
I would like to generate a random string of only lower-case ASCII characters (meaning lower case letters, numbers, and other ASCII characters; just no upper-case lettesr).
The max length of the string should be 587 characters (including null terminator).
How would I go about doing this?
Thanks
#define N 588
#include <stdlib.h>
#include <time.h>
void gen(char *dst)
{
int i, n;
srand(time(NULL)); /* init seed */
if ((dst = malloc(N)) == NULL) /* caller will need to free this */
return;
for (i = 0; i < N; )
if ((n = rand()) < 'A' && n > 'Z')
dst[i++] = n;
dst[N - 1] = 0; /* null terminate the string */
}
I tried this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define BUFFER_SIZE 587 /* 587 including NULL so 0..585 and 586 is NULL */
int main(int argc, char** argv)
{
size_t i;
char buffer[BUFFER_SIZE];
int x;
srand((unsigned int)time(NULL));
memset(buffer, 0, sizeof(buffer));
for (i = 0; i < BUFFER_SIZE-1; i++)
{
/* Note: islower returns only a b c..x y z, isdigit 0..9 and isprint only printable characters */
do
{
x = rand() % 128 + 0; /* ASCII 0 to 127 */
}
while (!islower(x) && !isdigit(x) && !isprint(x));
buffer[i] = (char)x;
}
buffer[BUFFER_SIZE-1] = '\0';
printf("%s", buffer);
getchar();
return EXIT_SUCCESS;
}
This way you can do a random string with whatever characters you want. The only thing is you should put them in the function
#include <stdlib.h>
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
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
Hi,
I am trying to read a string into my code via the args[]-parameter, like I would do in Java.
So basically, this is what I want to do:
- read the String "machine" over launch-parameter
- go through every letter of that string in a loop
- while in the loop, check is current letter equals "e"
- if letter equals "e", replace it with "a"
- return edited string
This is the best way to phrase my elemental questions to C. So I'd be happy if you won't take this post offensive.
How could I implement that code?
Here's a solution that (almost) doesn't involve pointers, though you should really learn about pointers if you're going to do even moderately advanced C programming.
void replace_e_with_a(char str[])
{
int i, len = strlen(str);
for (i=0; i<len; i++) {
if (str[i] == e) str[i] = a;
}
}
int main(int argc, char *argv[]){
int i;
for (i = 1; i < argc; i++) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}
Here's something that should work.
#include <stdio.h>
void replace_e_with_a(char * str)
{
int i;
if (NULL != str)
while ('\0' != *str ) {
if (*str == 'e')
*str = 'a';
++str;
}
}
int main(int argc, char **argv){
int i;
for (i = 1; i < argc; ++i) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}