I have two Character arrays, I would like to make one 2 Dimensional array.
but the Character values seem to be causing a problem, in the way that I tried to initialize them in the 2D array.
what is the proper way to initialize this type of array?
The function "trumplar()" works fine, or as I would expect.
The 2D character array x[22][22] function "trumpsterFire()" fails to be initialized properly.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void trumplar(){
int len = 22;
char a[25]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3d,0x76,0x1e,0x38,0x38,0x6d,0x00};
char L[25]="0123456789abcdefghjlpsS";
int i;
for (i = 0; i <=len; i++){
char hit=L[i];
char urd=a[i];
printf("The %d, Value of a is:%c\t Hex val: %c\n",i,hit,urd);
}
}
void trumptsterFire(){
//int xlen = 22;
char x[22][22]={
{0x3f,0},{0x6,1},{0x5b,2},
{0x4f,3},{0x66,4},{0x6d,5},
{0x7d,6},{0x7,7},{0x7f,8},
{0x6f,9},{0x77,a},{0x7c,b},
{0x39,c},{0x5e,d},{0x79,e},
{0x71,f},{0x3d,g},{0x76,h}
,{0x1e,j},{0x38,l},{0x38,p},
{0x6d,s},{0x00,S}
};
}
int main(){
trumplar();
trumptsterFire();
return 0;
}
Use single qoute (') to assign a character.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void trumplar(){
int len = 22;
char a[25]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3d,0x76,0x1e,0x38,0x38,0x6d,0x00};
char L[25]="0123456789abcdefghjlpsS";
int i;
for (i = 0; i <=len; i++){
char hit=L[i];
char urd=a[i];
printf("The %d, Value of a is:%c\t Hex val: %c\n",i,hit,urd);
}
}
void trumptsterFire(){
//int xlen = 22;
char x[22][22]={
{0x3f,'0'},{0x6,'1'},{0x5b,'2'},
{0x4f,'3'},{0x66,'4'},{0x6d,'5'},
{0x7d,'6'},{0x7,'7'},{0x7f,'8'},
{0x6f,'9'},{0x77,'a'},{0x7c,'b'},
{0x39,'c'},{0x5e,'d'},{0x79,'e'},
{0x71,'f'},{0x3d,'g'},{0x76,'h'}
,{0x1e,'j'},{0x38,'l'},{0x38,'p'},
{0x6d,'s'},{0x00,'S'}
};
}
int main(){
trumplar();
trumptsterFire();
return 0
;
}
Related
I'm trying to compare two string arrays. I write a method which called compare this method compare two string arrays and if it find same element in this arrays it stores that string in "intersect" array which compare method will be return. But even thought this two array has same elements when I try to print f which I try to equalize "intersect" array it prints empty array. How to I fix it, could yo please help me?
#include <stdio.h>
#include <string.h>
char *compare ( char *course1[], char *course2[]);
int main()
{
char a1 [12][50] = {"10000000000","10000000010","20000000010","30000000030","30000000010","40000000010","50000000010","50000000020","60000000020","70000000010"};
char a2 [12][50] = {"70000000010","10000000000","60000000020","11000000010","31000000030","31000000010","80000000010","50000000010"};
char *f = compare(*a1,*a2);
for(int b=0;b<sizeof(*f);b++){
printf("%c\n",f[b]);
}
return 0;
}
char *compare ( char *course1[], char *course2[]){
static char intersect[12][500];
int i = 0;
int k = 0;
while(i<sizeof(*course1)){
int j = 0;
while(j<sizeof(*course2)){
int cmp = strcmp(course1[i],course2[j]);
if(cmp==0){
strcpy(intersect[k],course1[i]);
k++;
j++;
}
else{
j++;
}
}
i++;
}
return *intersect;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void initRandom() {
srand(time(NULL));
}
int intUniformRnd(int a, int b){
return a + rand() % (b-a+1);
}
const char* animaisQuatro[] = {"gato", "urso","vaca"};
int main() {
char quatro[4] = {'*' , '*' , '*', '*'};
initRandom();
printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);
for(int i=0;i<4;i++){
printf("%c", quatro[i]);
}
return 0;
}
I have this code that give me a random animal from the array const char* animaisQuatro[] = {"gato", "urso","vaca","lapa"}; from here
initRandom();
printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);
and then I want to put that random animal in another array letter by letter but I don't know how
First I reduced your code to a minimal and reproducible example (something you should do whenever you ask a question):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void initRandom() {
srand(time(NULL));
}
int intUniformRnd(int a, int b){
return a + rand() % (b-a+1);
}
const char* animaisQuatro[] = {"gato", "urso","vaca"};
int main() {
char quatro[4] = {'*' , '*' , '*', '*'};
initRandom();
printf("%s\n", animaisQuatro[intUniformRnd(0,2)]);
for(int i=0;i<4;i++){
printf("%c", quatro[i]);
}
return 0;
}
Then you can proceed like this:
int main() {
char quatro[4] = {'*' , '*' , '*', '*'};
initRandom();
// Get the animal name from a random position
char* name = animaisQuatro[intUniformRnd(0, 2)];
// Iterate four times
for (int i = 0; i < 4; i++) {
// Assign each `name` index to its respective `quatro` position
quatro[i] = name[i];
}
printf("%s", quatro);
return 0;
}
Tip: you can avoiding hardcoding the 2 when calling intUniformRnd. Note that
printf("%d\n", (int) sizeof(animaisQuatro));
printf("%d\n", (int) sizeof(char*));
printf("%d\n", (int) sizeof(animaisQuatro) / sizeof(char*));
outputs
24
8
3
Therefore, you can do
int length = (int) sizeof(animaisQuatro) / sizeof(char*);
int pos = intUniformRnd(0, length - 1);
This way, if you want to add more elements to animaisQuatro, you don't need to change the value inside intUniformRnd.
I want to put that random animal in other array letter by letter
To copy a string to another character array, code could use
// Risky
strcpy(quatro, animaisQuatro[intUniformRnd(0,2)]);
That would overflow quatro[] if it is too small and leads to undefined behavior. (Bad)
A better way to copy and prevent buffer overflow and alert of a failure:
int len = snprintf(quatro, sizeof quatro, "%s", animaisQuatro[intUniformRnd(0,2)]);
if (len >= sizeof quatro) {
fprintf(stderr, "quatro too small.\n");
}
Since C99 and selectively afterword, code could use a variable length array to form a right-size quatro array.
const char *animal = animaisQuatro[intUniformRnd(0,2)];
size_t sz = strlen(animal) + 1;
char quatro[sz];
strcpy(quatro, animal);
Yet since intUniformRnd[] is constant, no need to copy the text, just copy the address to a pointer:
const char *quatro = animaisQuatro[intUniformRnd(0,2)];
Is there a way to create a string of characters from a set of elements taken from a existing array?
Example: say I have an array of four characters
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
char abcd[] = "abcd";
string sabcd = "0";
}
Is there a way I can create strings with a subset of array elements, such as "ac", "cd" and so on?
Edited: added libraries for clarification.
You could always index the characters you wish your substring to have.
For example:
char adb[4] = {abcd[0], abcd[3], abcd[1], '\0'};
Another approach is to use strncpy(), like this:
#include <stdio.h>
#include <string.h>
int main (void)
{
char abcd[] = "abcd";
char bc[3] = "";
strncpy(bc, abcd + 1, 2);
puts(bc);
}
Output:
bc
you can iterate over the array elements like this:
char abcd[] = "abcd";
for(int i=0; i<4; i++){
for(int j=i; j<4; j++){
d = abcd[i] + abcd[j];
}
I want to modify a char by using a function and print it on the screen but my code cannot achieve this function. Here is my source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long M = 2147483647;
void IntroduceError(char k[],double p)
{
int i;
for ( i = 0; i < 8; i++) {
if ((double)random()/M <= p)
k[i] = 1;
}
}
int main(int argc, char *argv[])
{
char test[] = "11110000";
double rate = atof(argv[1]);
IntroduceError(test, rate);
printf("\nErrored codeword is : %s\n",test);
return 0;
}
k is a string i.e. array of characters, but you're assigning an integer value to it.
Instead of:
k[i] = 1;
You probably want:
k[i] = '1';
Also, you should call srandom at the start of your program to seed the random number generator, passing in at least the PID, i.e. srandom(getpid()); so that you don't get the same results every time.
get in the printf statement is not defined. If you change get with test it should compile.
i have the next code, in which i have a string "hello world" and it has to convert each character into its ASCII value, but instead of printing [68656C.....] it prints some white spaces right after the '[', like this [......68656C]. And i canĀ“t find the reason why
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,lon;
char *cod_maq,*c = {"hello world"},c[2];
lon = strlen(c);
cod_maq = (char*)malloc((lon+1)*sizeof(char));
for(i = 0;i < lon;i++)
{
sprintf(c,"%X",c[i]);
strcat(cod_maq,c);
}
printf("[%s]\n",cod_maq);
return 0;
}
thanks
The memory in your malloc already contains data that you need to reset.
malloc allocates memory it doesn't initialize it, so you just get random garbage that was there previously.
memset(cod_maq, 0, size_of_cod_maq)
sprintf(c,"%X",c[i]); : very bad.
fix to like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i,lon;
char *cod_maq, *c = {"hello world"};
lon = strlen(c);
cod_maq = (char*)malloc((lon*2 + 1) * sizeof(char));//2 required per character
for(i = 0; i < lon; i++){
sprintf(cod_maq + i*2, "%02X", c[i]);
}
printf("[%s]\n", cod_maq);
free(cod_maq);
return 0;
}