Char array comparison - c

I am trying to compare two arrays. Both of these arrays contain 15 characters. I want to see if they have the same element or not.
characters being read in to the array
Array1: ATGGAATTCTCGCTC
Array2: TTGGAATTCTAGCTC
These are both in the form of arrays and are being passed in as arrayt and arraymt. n_size and m_size are the size of each array.(this is because the arrays can be any length 1-15)
int mutations(char arrayt[],char arraymt[],int n_size,int m_size)
{
int i=0,mutation=0;
do{
for(i=0;i<n_size;i++)
{
if (arrayt[i]==arraymt[i])
{
mutation=1;
}
}
}while(n_size == m_size);
return mutation;
}
This is the code I have so far for finding the differences in the two arrays. Problem is I don't know of a way that I can return if there is a difference for each of the elements. 1 is for a change and 0 is no change.
If anything is unclear let me know and I will try and clarify.

The code you required may be:
int* mutations(char arrayt[],char arraymt[],int n_size,int m_size)
{
int i, size;
int* mutation;
// size = min(m_size,n_size)
if n_size>m_size
size=m_size;
else
size=n_size;
endif
mutation=*malloc( sizeof(int) * size); // allocate memory for return value
for(i=0;i<size;i++)
{
if (arrayt[i]==arraymt[i])
{
mutation[i]=1;
} else {
mutation[i]=0;
}
}
return mutation;
}

Let's use 15 bits of an unsigned to record the results.
unsigned mutations(char arrayt[], char arraymt[], int n_size, int m_size) {
int sizemin = min(n_size, m_size);
int sizemax = max(n_size, m_size);
unsigned dest = 0;
int i;
for (i = 0; i < sizemin; i++) {
if (arrayt[i] == arraymt[i])
dest |= 1 << i;
}
for (; i < sizemax; i++) {
dest |= 1 << i; // compares against empty values are set to 1
}
return dest;
}
void foo(void) {
const char *Array1 = "ATGGAATTCTCGCTC";
const char *Array2 = "TTGGAATTCTAGCTC";
unsigned result = mutations(Array1, Array2, strlen(Array1), strlen(Array1));
// LSBit contains the result of the compare of Array1[0] vs. Array1[1]
printf("%X\n", result);
}

void mutations(char arrayt[],char arraymt[], int *result, int max_index)
{
int i = 0;
for (i = 0; i < max_index; i++)
{
if (arrayt[i] == arraymt[i])
{
result[i] = 0;
continue;
}
else
{
result[i] = 1;
}
}
}
int main()
{
int i = 0;
int max_index;
int *res;
char arrayt[] = "aabbccduu";
char arraymt[] = "aabcccd";
max_index = strlen(arrayt) <= strlen(arraymt) ? strlen(arrayt) : strlen(arraymt);
res = calloc (1, sizeof(int) * max_index);
if (res != NULL)
{
mutations(arrayt, arraymt, res, max_index);
while (i < max_index)
{
printf("res[i] = %d\n", res[i]);
i++;
}
free(res);
}
return 0;
}

Related

Iterate Over Char Array Elements in C

This code is supposed to take a user's input and convert it to binary. The input is grouped into an integer array to store character codes and/or adjacent digits, then each item in the integer array is converted to binary. When the user types "c357", "c" should be converted to 99, then converted to binary. Then, "357" should be converted to binary as well. In the main() function, strlen(convert) does not accurately represent the number of items in array convert, thus only iterating over the first array item.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#define EIGHT_BITS 255
#define SIXTEEN_BITS 65535
#define THIRTY_TWO_BITS 4294967295UL
// DETERMINE NUMBER OF BITS TO OUTPUT
int getBitLength(unsigned long d) {
int l;
if (d <= EIGHT_BITS) {
l = 8;
}
else if (d > EIGHT_BITS && d <= SIXTEEN_BITS) {
l = 16;
}
else if (d > SIXTEEN_BITS && d <= THIRTY_TWO_BITS) {
l = 32;
}
return l;
}
// CONVERT INPUT TO BINARY VALUE
char* convertToBinary(unsigned long int decimal) {
int l = getBitLength(decimal);
static char b[33];
char bin[33];
int i, j, k = 0, r;
b[33] = '\0';
bin[33] = '\0';
printf("Bits................ %ld\n", l);
// creates array
for (i = 0; i < l; i++) {
r = decimal % 2;
decimal /= 2;
b[i] = r;
}
// reverses array for binary value
for (j = l - 1; j >= 0; j--) {
bin[k] = b[j];
strncpy(&bin[k], &b[j], l);
snprintf(&bin[k], l, "%d", b[j]);
k++;
}
printf("Binary Value: %s\n", bin);
return bin;
}
unsigned long int* numbersToConvert(char* input) {
const int MAX_INPUT = 20;
int i, k = 0, z = 0;
char numbers[MAX_INPUT];
unsigned long int *toConvert = malloc(MAX_INPUT * sizeof(int));
numbers[MAX_INPUT] = '\0';
for (i = 0; i < strlen(input); i++) {
if (isdigit(input[i])) {
numbers[z] = input[i];
if (!isdigit(input[i + 1])) {
toConvert[k] = strtol(numbers, NULL, 10);
printf("----- %ld -----\n", toConvert[k]);
z = 0;
}
else {
z++;
}
}
else {
printf("----- %c -----\n", input[i]);
printf("Character Code: %d\n", input[i]);
toConvert[k] = (unsigned long int) input[i];
}
k++;
}
return toConvert;
}
int main(void) {
const int MAX_INPUT = 20;
int i, p;
char input[MAX_INPUT];
unsigned long int* convert;
printf("------- Input --------\n");
scanf("%s", input);
input[MAX_INPUT] = '\0';
// PRINT INPUT AND SIZE
printf("\nInput: %s\n", input);
convert = numbersToConvert(input);
convert[MAX_INPUT] = '\0';
printf("strlen: %ld\n", strlen(convert));
for (i = 0; i < strlen(convert); i++) {
printf("num array: %ld\n", convert[i]);
convertToBinary(convert[i]);
}
return 0;
}
I have attempted to null terminate each string to prevent undefined behavior. I am unsure if certain variables, if any, are meant to be static.
It is hard to read your code.
Here you have something working (converting the number to binary):
static char *reverse(char *str)
{
char *end = str + strlen(str) - 1;
char *saved = str;
int ch;
while(end > str)
{
ch = *end;
*end-- = *str;
*str++ = ch;
}
return saved;
}
char *tostr(char *buff, unsigned long long val)
{
if(buff)
{
char *cpos = buff;
while(val)
{
*cpos++ = (val & 1) + '0';
val >>= 1;
}
*cpos = 0;
reverse(buff);
}
return buff;
}
int main()
{
char buff[128];
printf("%s\n", tostr(buff, 128));
}
https://godbolt.org/z/6sRC4C

Checking if an array contains all the elements

I wrote a Code that reads from a txt.file stores it into an array , remove the spaces then print them out.
I want to add another functionality. This time is to check if the user provided the right input file.
I want to compare the array reds with the array stringcard and see if the array red contains all the elements of the array stringcard.
I have been searching on the internet for a while but I don't know how to solve this problem.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max 13
#define stringlength 8
const char *stringcard[] = {
"REDA",
"RED2",
"RED3",
"RED4",
"RED5",
"RED6",
"RED7",
"RED8",
"RED9",
"RED10",
"REDJ",
"REDQ",
"REDK",
};
char * removechar(char str[], int ch) {
char * cpos = str;
while ((cpos = strchr(cpos, ch))) {
strcpy(cpos, cpos + 1);
}
return str;
}
int main(int argc, char ** argv) {
char * reds[max];
int i;
FILE * file = argc > 1 ? fopen(argv[1], "r") : stdin;
if (file == NULL)
return 1;
if (argc != 2) {
printf("[ERR]");
return 0;
}
for (i = 0; i < max; i++) {
reds[i] = malloc(stringlength);
fgets(reds[i], stringlength, file);
}
for (i = 0; i < max; i++) {
printf("%s", reds[i]);
}
// removes spaces
for (i = 0; i < max; i++) {
removechar(reds[i], ' ');
}
for (i = 0; i < max; i++) {
printf("%s", reds[i]);
}
int success = 1;
size_t size = sizeof(stringcard)/sizeof(stringcard[0]);
size_t size2 = sizeof(reds)/sizeof(reds[0]);
if(size == size2)
{
for(int i = 0; i<size;i++)
{
if(strcmp(stringcard[i], reds[i]) != 0){
success = 0;
printf("nope");
break;
}
}
}
return 0;
}
Input:
RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K
This is prefaced by my top comments.
This should work for cards in any order:
size_t size = sizeof(stringcard) / sizeof(stringcard[0]);
size_t size2 = sizeof(reds) / sizeof(reds[0]);
int allmatch = 0;
if (size == size2) {
allmatch = 1;
for (int i = 0; i < size; ++i) {
int curmatch = 0;
const char *curcard = &stringcard[i];
for (int j = 0; j < size; ++j) {
if (strcmp(curcard, reds[j]) == 0) {
curmatch = 1;
break;
}
}
if (! curmatch) {
allmatch = 0;
break;
}
}
}
printf("RESULT: %s\n",allmatch ? "MATCH" : "NOMATCH");
UPDATE:
if we know reds is sorted compare the result of strcmp with -1 or 1 rather than 0 depending on the order of the sorted elements. If stringcard is sorted of course exchange the roles
Okay, if we assume stringcard is always sorted [or we choose to pre-sort it], we can add an early escape to the inner loop that can save a small amount of time for the failed case:
size_t size = sizeof(stringcard) / sizeof(stringcard[0]);
size_t size2 = sizeof(reds) / sizeof(reds[0]);
int allmatch = 0;
if (size == size2) {
allmatch = 1;
for (int i = 0; i < size; ++i) {
int curmatch = 0;
char *redcard = reds[i];
for (int j = 0; j < size; ++j) {
int cmpflg = strcmp(redcard,stringcard[j]);
if (cmpflg == 0) {
curmatch = 1;
break;
}
if (cmpflg > 0)
break;
}
if (! curmatch) {
allmatch = 0;
break;
}
}
}
printf("RESULT: %s\n",allmatch ? "MATCH" : "NOMATCH");
Always try to use functions when possible.
int all_match(const char **table1, const char **table2, size_t t1size, size_t t2size)
{
for(size_t t1index = 0; t1index < t1size; t1index++)
{
int match = 0;
for(size_t t2index = 0; t2index < t2size; t2index++)
{
match = match || !strcmp(table1[t1index], table2[t2index]);
if(match) break;
}
if(!match) return 0;
}
return 1;
}
Given your other array is defined similarly, say inputArray, and both arrays are sorted before executing the following, then you could use code similar to: (including number of elements
//Assumes both arrays are sorted before executing the following
char success = 1;
size_t size = sizeof(stringcard)/sizeof(stringcard[0]);
size_t size2 = sizeof(inputArray)/sizeof(inputArray[0]);
if(size == size2)
{
for(int i = 0; i<size;i++)
{
if(strcmp(stringcard[i], inputArray[i]) != 0)
{
success = 0;
break;
}
}
{
else
{
success = 0;
}
//Proceed according to the value of success,
...

can't able to return pointer value of function to main

link of program question https://www.hackerrank.com/challenges/cut-the-sticks/problem
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char** split_string(char*);
int* cutTheSticks(int arr_count, int* arr, int* result_count) {
int c=arr_count,min;
int result[c];
for (int k=0;k<arr_count;k++){
min = *arr;
for (int i = 0; i < c; i++) {
if (*(arr + i) < min)
min = *(arr + i);
}
int count=0;
for (int i=0; i < c; i++) {
if(min==*(arr+i))count++;
}
for (int i=0; i < c; i++) {
*(arr+i)=*(arr+i)-min;
}
int temp=0;
for(int i=0;i<c;i++){
for(int x=i;x<c;x++){
if(*(arr+i)<*(arr+x)){
temp=*(arr+i);
*(arr+i)=*(arr+x);
*(arr+x)=temp;
}
}
}
result[k]= c;
*(result_count)=*(result_count)+1;
c = c - count;
if(c==0)break;
}
return result;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
char** arr_temp = split_string(readline());
int* arr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
char* arr_item_endptr;
char* arr_item_str = *(arr_temp + i);
int arr_item = strtol(arr_item_str, &arr_item_endptr, 10);
if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') {
exit(EXIT_FAILURE); }
*(arr + i) = arr_item;
}
int arr_count = n;
int result_count;
int* result = cutTheSticks(arr_count, arr, &result_count);
for (int i = 0; i < result_count; i++) {
fprintf(fptr, "%d", *(result + i));
if (i != result_count - 1) {
fprintf(fptr, "\n");
}
}
fprintf(fptr, "\n");
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
This above code is my full code from harkerrank, below is main fuction.
i can't able to return my pointer value from cutTheStricks to main function.
// Please store the size of the integer array to be returned in
result_count
pointer. For example,
// int a[3] = {1, 2, 3};
//
// *result_count = 3;
//
// return a;
int* cutTheSticks(int arr_count, int* arr, int* result_count) {
int c=arr_count,min;
int result[c];
for (int k=0;k<arr_count;k++){
min = *arr;
for (int i = 0; i < c; i++) {
if (*(arr + i) < min)
min = *(arr + i);
}
int count=0;
for (int i=0; i < c; i++) {
if(min==*(arr+i))count++;
}
for (int i=0; i < c; i++) {
*(arr+i)=*(arr+i)-min;
}
int temp=0;
for(int i=0;i<c;i++){
for(int x=i;x<c;x++){
if(*(arr+i)<*(arr+x)){
temp=*(arr+i);
*(arr+i)=*(arr+x);
*(arr+x)=temp;
}
}
}
result[k]= c;
*(result_count)=*(result_count)+1;
c = c - count;
if(c==0)break;
}
return result;
}
I am not getting any output but my logic is correct.
I know there is there is something missing therefore i can't able to return my output;
I can't figure out which pointer value(*result or *result_count ) is not passing
or what is happening i can't able to figure out.
You are returning the address of a local array whose life time ends with the function, change
int result[c];
to
int *result = malloc(sizeof(*result) * c);
You are also using the address of an uninitialized value here:
*(result_count)=*(result_count)+1;
change
int result_count;
to
int result_count = 0;

Learning C trying to figure out this malloc stuff via functions

Hey I'm trying to figure out why the following code gets invalid write of size error from Valgrind at the line: array[i-1] = I;
I really don't now why my allocate_array function doesn't work. I tried so many things.
There are couple errors more but I just wanted to check first why this line is false or why my array isn't allocated.
Hope you can help me to figure out my error.
#include<stdio.h>
#include<stdlib.h>
//Programm to check Gaussian function
int read_number_from_stdin(int* value) {
printf("Number for the Gaussian Function: ");
int return_value = scanf("%d", value);
if (return_value == 0) {
while (fgetc(stdin) != '\n')
;
}
if (return_value == EOF) {
return_value = 0;
}
return return_value;
}
int read_number_from_string(char* string, int* value) {
printf("Reading input...\n");
int return_value = sscanf(string, "%d", value);
if (return_value == 0 || return_value == EOF) {
printf("\t... Error your input is not a Number!\n");
return_value = 0;
} else {
printf("\t... Number %d read and saved.\n", *value);
}
return return_value;
}
int* allocate_array(int* size) //allocating memory for the array
{
int* result = (int*) malloc(sizeof(int) * (*size));
return result;
}
void initialize_array(int array[], int size) {
for (int i = 0; i < size; i++) {
array[i] = i+1;
}
}
int compute_sum_and_place_in_first_elem(int array[], int* size) {
int sum_array = 0;
for (int i = 0; i < *size; i++) {
sum_array += array[i];
}
return sum_array;
}
void free_memory(int array[], int* N) {
free(array);
free(N);
}
int main(int argc, char* argv[]) {
int* N = malloc(sizeof(int));
if (argc == 1) {
while (read_number_from_stdin(N) != 1)
;
} else if (argc == 2) {
if (read_number_from_string(argv[1], N) == 0) {
printf("Error: No valid number!\n", argv[1]);
return -1;
}
} else {
printf("No valid number!\n");
return -1;
}
int* array = allocate_array(N); //allocate via function
initialize_array(array, *N); //initialize the array up to n
int result = compute_sum_and_place_in_first_elem(array, N);
int result_gauss = ((*N + 1) * (*N) / 2);
if (result == result_gauss) {
printf("Gauss was right your calculations match with his function");
} else {
printf(
"\nGauss was not right!\n"
"The summ of %d is %d and therefore not equal to(%d+1)*%d/2\n\n",
*N, result, *N, *N);
}
//free memory
free_memory(array, N);
}
As I can see, for the initialize_array() function, for the for loop, very first iteration, i is 0, and you're executing
array[i-1] = i;
which translates to
array [-1] = ....
which is illegal.
You can fix that using the default C-array property of 0-based indexing scheme. Something like
for(int i = 0; i < size; ++i)
{
array[i] = i;
}

How to set variable value by your index on RAM?

I receive a pointer numeric value like: 0xbfe0e6ac it is possible set int foo for example to value of 0xbfe0e6ac that is another int in memory?
My scenario:
I am tried to implement functions for get next index from array passing the pointer value (like the above example) as parameter.
For example:
char * a = "baa";
char* b = "foo";
printf("%c", cgetnext(&a)); // b
printf("%c", cgetnext(&a)); // a
printf("%c", cgetnext(&b)); // f
printf("%c", cgetnext(&b)); // a
The single way that I can see to do this is: in cgetnex() save pointer of array passed like parameter into array and each call, check if the array passed as paramater was used before and get information of it, like lasindexused and pointer value into RAM.
Here is my code:
#include <string.h>
#include <stdlib.h>
typedef struct { int pLocation; int lastIndex; } POINTERINFORMATION;
int __myIndex;
POINTERINFORMATION pointersLocations[256];
int pointersLocationsLength;
char * temparr = NULL;
__myIndex = pointersLocationsLength = 0;
int
plAdd (int p)
{
if (plLen >= sizeof(pointersLocation)) {
return -1;
} else {
pointersLocation[pointersLocationsLength].pLocation = p;
pointersLocation[pointersLocationsLength].lastindex = 0;
pointersLocationsLength ++;
return pointersLocationsLength;
}
}
int
getPointer (int p, POINTERINFORMATION ** out)
{
int i;
for (i = 0; i < pointersLocationsLength; i++)
{
if(pointersLocation[pointersLocationsLength].pLocation == p)
{
*out = makevariablefromponiter(pi.pLocation);
return 1;
}
}
return 0;
}
char
cgetnext(char * arr)
{
char * myarr;
const size_t size = sizeof(char *) + 1;
myarr = malloc(size);
if (NULL == arr){
POINTERINFORMATION * pi;
if (getPointer(p, &pi))
{
__myIndex = pi.lastindex;
myarr = makevariablefromponiter(pi.pLocation);
}
} else {
myarr = strdup (arr);
}
if (strlen(myarr) == __myIndex) {
return '\0';
} else {
__myIndex ++;
temparr = malloc(size);
temparr = strdup(myarr);
return myarr[__myIndex - 1];
}
}
Yes, do something like this:
int *fooPtr = 0xbfe0e6ac;
foo = *fooPtr;
In fact, you can make it even more concise:
foo = *(int *) 0xbfe0e6ac;

Resources