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;
}
Related
Write a function common_char that takes two strings as arguments and returns a new string that contains a single copy of all characters that appear in either of the two strings.
For example, string1: hello; string2: world; the new string is : hellowrd (o and l were already in array from hello).
May use string function here.In other words, all characters in string1 are copied into the new string, but characters in string 2 are copied only characters that are not in string1. That is past exam question and the university did not provide answer. Here is my code.
#include <stdio.h>
#include <string.h>
char *common_char(char *string1, char *string2) {
int str_length1 = strlen(string1);
int str_length2 = strlen(string2);
char *new_string = malloc(str_length1+str_length2+1);
for (int index_1 = 0; index_1 < str_length1; index_1++) {
for (int index_2 = 0; index_2 < str_length2; index_2++) {
if (string1[index_1] == string2[index_2]) {
}
}
}
}
int main(void) {
return 0;
}
My idea is to find duplicate characters in string 2 and string 1 according to the nested loop, but there is a problem with the conditional statement, there is red line, also how to copy the character of the non-duplicate string? I know strcopy(), but how to remove the repeated characters?
I've come up with a solution that uses dynamic memory and resizes the result char* each time a new char must be added. There are two loops, the first iterates the b string and the second loop checks that non of char of the b string is repeated in the a string, if it is not repeated, then adds it. Hope you understand the realloc to resize dynamically the char* each time it must be added an element.
Firstly I initialize the result string to the size of string a so it can be all copied inside. The ordering method I think it is called bubble method.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* common_char(char* a, char* b) {
char* result = (char*)malloc(sizeof(char)*strlen(a)+1);
int i = 0;
int j = 0;
int repeated = 0;
strcpy(result,a);
for(i=0; i<strlen(b); i++) {
for(j=0; j<strlen(result); j++) {
if(b[i] == a[j]) {
repeated = 1;
}
}
if(!repeated) {
result = (char*)realloc(result,strlen(result)+sizeof(char));
result[strlen(result)] = b[i];
result[strlen(result)+1] = '\0';
}
repeated = 0;
}
return result;
}
int main()
{
char a[] = "hello";
char b[] = "world";
char* result = common_char(a,b);
printf("%s", result);
return 0;
}
EDIT: I've modified the code to make it function. About the comment of memory allocation, I've modified the declaration of result to give it space for the '\0'. When doing the realloc, I've already considered that the realloc does not increment the strlen() because strlen() is a counter till the '\0' not of the size of the variable.
I am new in C programming. I would like to ask how I can store a char in a char array? The reason why I want to do it is because I get a char from a computation I make. This computation returns a char consisting of 4. I want to look through every character of the 4 and check if they are "0". If not, then I will increment by one. This is an example of a substitution: "0xf3"- I am trying to compute the Hamming Weight of a substitution. This is how I try to count how many non-zero the char consists of:
#include <stdio.h>
int main()
{
char x = "0xf3";
char s[10] = x;
int lingh = strlen(s);
for (int i = 0; i<lingh;i++) {
printf("%c\n", s[i] );
}
return 0;
}
the output I expect is something like s[] = {0,x,f,3}
This computation returns a char consisting of 4.
You are saying your computation returns a character which consists of 4 characters. Is it possible???
I guess your computation returns a string that consists of 4 characters. Example 0xf3
You can not store a string literal in char type variable.
char x = "0xf3"; //<---------------------
You can store them in a character array like char x[] = "0xf3";
Here is the modified code
#include <stdio.h>
#include<string.h> //<---- for strlen() and strcpy()
int main()
{
char x[] = "0xf3"; //<----store in a char array
char s[10];
strcpy(s, x); //<----copy in another array
int lingh = strlen(s);
for (int i = 0; i<lingh;i++) {
printf("%c\n", s[i] );
}
return 0;
}
I am trying to write a program for my class but I can't get started because I don't know how to access the function's argument elements. A char array is passed into the function like this:
RPN_calculator(input1)
where input1 is a pointer to an array and the function starts like this
int RPN_calculator(const char** input)
{
int n = strlen(*input);
printf("LENGTH OF INPUT: %d\n", n);
return 0;
}
I was trying to find the length of the array and then iterate through the array but anything I've tried does not print the right length of the array and I can't seem to figure out how to access any of the elements of 'input' (the print statement was just for debugging)
EDIT:
even when I calculate n as:
int n = sizeof(*input)/sizeof(*input[0]);
it doesn't work
I hope this source code will help you with the issue. It is a simple example program that demonstrates how to access any of the strings character by character and also how to find the size of the strings.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMBER_OS_CHAR_STRINGS 5
/* Here input is a pointer to an array of strings */
const char *input[NUMBER_OS_CHAR_STRINGS] = {
"ONE", /*string0. string[0][0] = 'O' -> first element of string0 - O
string[0][1] = 'N' -> second element of string0 - N
string[0][2] = 'E' -> third element of string0 - E */
"TWO", /*string1*/
"THREE", /*string2*/
"FOUR", /*string3*/
"FIVE", /*string4*/
};
int RPN_calculator(const char **input);
void itterate (const char **input, int choosen_string);
int main(void) {
int string_to_itterate = 0;
RPN_calculator(input);
printf("Select the string which you would like to print char by char:\n");
scanf("%d", &string_to_itterate);
itterate(input, string_to_itterate);
return(0);
}
int RPN_calculator(const char** input)
{
int i;
for (i = 0; i < NUMBER_OS_CHAR_STRINGS; i++)
{
int n = strlen(input[i]);
printf("LENGTH OF INPUT: %d\n", n);
}
return 0;
}
/*Simple example function which itterates throught the elements of a chossen string and prints them
one by one.*/
void itterate (const char **input, int choosen_string)
{
int i;
/*Here we get the size of the string which will be used to define the boundries of the for loop*/
int n = strlen(input[choosen_string]);
for (i = 0; i < n; i++)
{
printf("The %d character of string[%d] is: %c\n",i+1, choosen_string, input[choosen_string][i] ); /*Here we print each character of the string */
}
return;
}
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
;
}
How do you make 2 array strings into 1 array string, where I can print out all the 52 playing cards?
my code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main() {
char deck[52];
char suits[] = {"Hearts","Diamonds","Clubs","Spades"};
char values[]= {"Ace","Two","Three","Four","Five","Six",\
"Seven","Eight","Nine","Ten","Jack",\
"Queen","King"};
int V, S, d = 0;
char string;
for ( S= 0; S <4; S++) {
for (V =0; V< 13; V++) {
string = strcat( values[V], suits[S]);
deck[d] = string;
printf("%s\n", string);//prints out all the 52 playing cards
d++;
}
}
return 0;
}
When I executed the program, the problem comes up which asks me to debug the program or close the program, where I closed the program in the end, which returns nothing. Can you please give me the answer which works?
Check the below code which fixes the issues in your code:
The problem with your code is you try to modify the actual string before printing and because of this there is a modified string in the next iteration. So just copy the values and suits to array and print it out as shown below.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main()
{
int i=0;
char deck[30] = "";
char suits[][30] = {"Hearts","Diamonds","Clubs","Spades"};
char values[][30]= {"Ace","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack",
"Queen","King"};
int V, S;
for ( S= 0; S <13; S++)
{
for (V =0; V< 4; V++){
memset(deck,0,sizeof(deck));/* Clear the buffer before writing new value*/
strcpy( deck, values[S]);
strcat(deck,suits[V]);
printf("%s\n", deck);//prints out all the 52 playing cards
i++;
}
}
printf("Number of playing cards: %d\n",i);
return 0;
}
strcat() returns a char *, a pointer to a char, not a char.
You are not even required to even consider the return value of strcat() since the destination pointer (first argument) will now contain the concatenated string, assuming enough memory is already allocated.
So here in your code, you are trying to put the concatenated string to values[V] which could fail when memory already allocated to it becomes insufficient.
The best method would be to allocate some memory (as you did with deck[]) and set it all to zeroes. Then keep strcat()ing there.
strcat(deck, values[V]);
strcat(deck, suits[S]);
An alternative to using strcpy and strcat is to use sprintf.
#include<stdio.h>
#include<string.h>
#define NUM_SUITS 4
#define CARDS_PER_SUIT 13
#define TOTAL_CARDS (NUM_SUITS * CARDS_PER_SUIT)
int main()
{
char deck[TOTAL_CARDS][24];
char* suits[NUM_SUITS] = {"Hearts","Diamonds","Clubs","Spades"};
char* values[CARDS_PER_SUIT]= {"Ace","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack",
"Queen","King"};
int s, c, i;
for(s = 0; s < NUM_SUITS; s++)
{
for(c = 0; c < CARDS_PER_SUIT; c++)
{
sprintf(deck[(s * CARDS_PER_SUIT) + c], "%s of %s", values[c], suits[s]);
}
}
for(i = 0; i < TOTAL_CARDS; i++)
{
printf("%s\n", deck[i]);
}
return 0;
}