Compare arrays of words in C - c

How I can compare 2d arrays of char in C?
I tried this
Char **arr_1;
Char **arr_2;
...// malloc. Its OK.
for (I=0; I<n; I++)
{
If (strcmp (arr_1[I],arr_2[I])==0)
// do smth
}
But it doesn't work . I'm not good in pointers.
Arrays have some array of words.
Ubuntu gcc
Without strcmp, the program works.
Thanks

You can do it.
char arr_1[10][10];
char arr_2[10][10];
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf(" %[^\n]",arr_1[i]);
scanf(" %[^\n]",arr_2[i]);
}
for(int I=0; I<n; I++)
{
if(strcmp (arr_1[I],arr_2[I])==0)
{
// do smth
}
}

you must be allocating 2D dynamic array wrong.
consider this code:
int SIZE=5;
char **arr_1;
arr_1 = malloc(SIZE* sizeof(char *)); //initialising an array of pointers
char **arr_2;
arr_2 = malloc(SIZE* sizeof(char *)); //initialising an array of pointers
for(i=0;i<SIZE;i++)
{
printf(" Enter a name\n");
arr_1[i]=malloc(100*sizeof(char)); //for each pointer in this array allocate an array of characters
scanf("%99s",arr_1[i]);
}
for(i=0;i<SIZE;i++)
{
printf(" Enter a name\n");
arr_2[i]=malloc(100*sizeof(char)); //for each pointer in this array allocate an array of characters
scanf("%99s",arr_2[i]);
}
for(i=0;i<SIZE;i++)
{
if(strcmp(arr_1[i],arr_2[i])==0)
//do smthing
}

Related

C user input getting skipped

I wrote a code in C, which utilizes dynamic memory allocation but my input gets skipped second iteration onwards.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of users :");
scanf("%d", &n);
// char **array = malloc(n * sizeof(char *));
char flag[4];
for(int i=0;i<n;i++) {
printf("Does user have middle name? (yes/no) :");
scanf("%s", flag);
if (strcmp("yes", flag)==0) {
char **array = malloc(3 * sizeof(char *));
for(int j=0;j<3;j++)
array[i] = (char *)malloc(9);
printf("First name :");
scanf("%s", array[0]);
printf("Middle name :");
scanf("%s", array[1]);
printf("Last name :");
scanf("%s", array[2]);
for(int i=0;i<3;i++)
free(array[i]);
free(array);
}
else {
char **array = malloc(2 * sizeof(char *));
for(int j=0;j<2;j++)
array[i] = (char *)malloc(9);
printf("First name :");
scanf("%s", array[0]);
printf("Last name :");
scanf("%s", array[1]);
for(int i=0;i<2;i++)
free(array[i]);
free(array);
}
}
}
Here, say I give n=3 and enter "yes". It inputs the first, middle, last name but then skips the next "Does it have a middle name". Why is this happening??
The problem is here:
for(int j=0;j<3;j++)
array[i] = (char *)malloc(9);
you want to use the index j, not i:
for(int j=0;j<3;j++)
array[j] = malloc(9); // Don't cast malloc
You are also shadowing i in the inner loop:
for(int i=0;i<n;i++) {
...
for(int i=0;i<3;i++)

printing string from the array

as you can see this while I run this it works perfectly till "The array is " but cant show the string that I enter
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
int size;
char arr[size];
printf("input size of array ");
scanf("%d", &size);
printf("\ninput elements of array ");
for (int i = 0; i < size; i++)
{
scanf("%s", &arr[i]);
}
printf("\nThe array is ");
for (int i = 0; i < size; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
There are multiple things wrong with this code.
int size;
char arr[size];
You are declaring a char array arr with size elements, but size is not yet initialized yet and may be any value. Also note that "deciding the size of an array at runtime" is only valid in modern C (C99 onwards).
You should first read size and only after the scanf declare arr.
for (int i = 0; i < size; i++){
scanf("%s", &arr[i]);
}
You read a string (%s) with scanf and try to store it in a char (&arr[i] points to the ith char in arr). Every C string is atleast 1 character long (the terminating \0 character), you are trying to store multiple characters in a single char.
Instead use
scanf("%s", arr);
Note that scanf is not safe. Even if you enter more than size characters, it will still try to store them in arr. That leads to undefined behaviour, because arr is too small. Instead you can use fgets, that lets you set the number of characters to read.
for (int i = 0; i < size; i++){
printf("%s\n", arr[i]);
}
Here you are trying to print a String (%s), but you are passing a char (arr[i] is the ith char in arr).
Instead use
printf("%s\n", arr);

Dynamically allocate an array of strings

How can I fix this code in a way that it prints the words in the array? Moreover this is the right way to dynamically allocate memory for n words of max size 40?
int main() {
int n;
char *arr;
int i;
printf("Give me a number:");
scanf("%d", &n);
arr = malloc(n * 40);
for (i = 0; i < n; i++)
{
printf("Give me a word: ");
scanf("%s", &arr[i]);
}
for (i = 0; i < n; i++)
{
printf("%s", arr[i]); //< --problem here
}
return 0;
}
Your allocation is not the best, and printf argument arr[i] expects a char* but you pass it an int (a char if you'd like).
Here is how you should do it, with comments:
Live demo
#include <stdio.h>
#include <stdlib.h> //for malloc
int main(){
int n;
int i;
printf("Give me a number:");
scanf("%d", &n);
//declare a variable of pointer to pointer to char
//allocate memory for the array of pointers to char,
//each one capable of pointing to a char array
char **arr = malloc(n * sizeof *arr);
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
//allocate memory for each individual char array
for(i = 0; i < n; i++){
arr[i] = malloc(40); //char size is always 1 byte
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
}
for (i = 0; i < n; i++){
printf("Give me a word: ");
//limit the size of read input to 39 charaters to avoid overflow,
//a nul character will be added by scanf
scanf("%39s", arr[i]);
}
for (i = 0; i < n; i++){
printf("%s\n", arr[i]);
}
for(int i = 0; i < n; i++){ //free the memory for each char array
free(arr[i]);
}
free(arr); //free array of pointers
return 0;
}
You can also do this with less code using a pointer to array of 40 chars, this will simplify the memory allocation and deallocation:
Sample with comments:
Live demo
#include <stdio.h>
#include <stdlib.h> //for malloc
int main(){
int n;
int i;
printf("Give me a number:");
scanf("%d", &n);
//declare a pointer to array of chars and
//allocate memory for all the char arrays
char (*arr)[40] = malloc(n * sizeof *arr);
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
for (i = 0; i < n; i++){
printf("Give me a word: ");
scanf("%39s", arr[i]);
}
for (i = 0; i < n; i++){
printf("%s\n", arr[i]);
}
free(arr); //free allocated memory
return 0;
}
This:
for(i=0;i<n;i++){
printf("Give me a word: ");
scanf("%s",&arr[i]);
}
is probably not what you want.
You probably want this instead:
for(i=0; i<n; i++){
printf("Give me a word: ");
scanf("%s", arr + i*40);
}
then later:
for(i=0; i<n; i++){
printf("%s", arr + i*40);
}
Remember that a string in C is just an array of characters.
Thus when defining char *arr, you are creating a single string. Had you done char **arr it would have been an array of strings, which is what you want.
However, I find that allocating/freeing arrays of arrays on the heap to be rather inconvenient, and prefer 'flattening' them into a single array.
This is exactly what you were doing with arr = malloc(n*40), but then later you treated this array of characters as an array of strings when you did this:
for(i=0; i<n; i++){
printf("Give me a word: ");
scanf("%s", &arr[i]);
}
Note that this is actually perfectly legal (scanf wanted a char* and you gave it one), but it's a logical error since you are giving it the n-th character when you wanted to give it the n-th array.
And oh yes, don't forget to free(arr) later.

Return 2d char array in C

I want to take a 2D char array as an input in a function (actually my 2D array is global and the function doesn't have inputs) , change the values in it and then return another 2D char array.
char stoixeia[50][7];
int main(int argc, char *argv[])
.
.
.
if (strcmp(answer, "Gift")==0)
{
gift();
}
char gift ()
{
int i,j,m;
int wrong=0;
int k=0;
char usern[50];
while(wrong=0)
{
printf("Enter the username that you want to Gift:\n");
scanf("%s", &usern);
for (i=0; i<50; i++)
{
if (*usern==stoixeia[i][0])
{
wrong=1;
k=i;
}
}
}
m=strlen(usern);
for(i=0; i<m; i++)
{
stoixeia[k][6]= stoixeia[k][6] + 10;
}
return stoixeia[50][7];
}
My thought was that if i declare my array as a global one everything would change in my functions and the array will get "updated". The compiler doesn't show any errors but when I run the programm and my answer is Gift the .exe stops working. Can you suggest me anything? Thank you
your function must be like this:
You don't need to return a value because you change directly the global variable.
void gift ()
{
int i,j,m;
int wrong=0;
int k=0;
char usern[50];
while(wrong==0) /* replace = by ==*/
{
printf("Enter the username that you want to Gift:\n");
scanf("%s", usern);
for (i=0; i<50; i++)
{
if (usern[i]==stoixeia[i][0])
{
wrong=1;
k=i;
}
}
}
m=strlen(usern);
for(i=0; i<m; i++)
{
stoixeia[k][6]= stoixeia[k][6] + 10;
}
}
As already mentioned use **gift() see here for some more information

how to store and then print a 2d character/string array?

Suppose I have the words: tiger, lion, giraffe.
How can I store it in a two dimensional char array using for loop and scanf and then print the words one by one using a for loop?
Something like
for(i=0;i<W;i++)
{
scanf("%s",str[i][0]); //to input the string
}
PS Sorry for asking such a basic question, but I couldn't find a suitable answer on Google.
First you need to create an array of strings.
char arrayOfWords[NUMBER_OF_WORDS][MAX_SIZE_OF_WORD];
Then, you need to enter the string into the array
int i;
for (i=0; i<NUMBER_OF_WORDS; i++) {
scanf ("%s" , arrayOfWords[i]);
}
Finally in oreder to print them use
for (i=0; i<NUMBER_OF_WORDS; i++) {
printf ("%s" , arrayOfWords[i]);
}
char * str[NumberOfWords];
str[0] = malloc(sizeof(char) * lengthOfWord + 1); //Add 1 for null byte;
memcpy(str[0], "myliteral\0");
//Initialize more;
for(int i = 0; i < NumberOfWords; i++){
scanf("%s", str[i]);
}
You can do this way.
1)Create an array of character pointers.
2)Allocate the memory dynamically.
3)Get the data through scanf. A simple implementation is below
#include<stdio.h>
#include<malloc.h>
int main()
{
char *str[3];
int i;
int num;
for(i=0;i<3;i++)
{
printf("\n No of charecters in the word : ");
scanf("%d",&num);
str[i]=(char *)malloc((num+1)*sizeof(char));
scanf("%s",str[i]);
}
for(i=0;i<3;i++) //to print the same
{
printf("\n %s",str[i]);
}
}
#include<stdio.h>
int main()
{
char str[6][10] ;
int i , j ;
for(i = 0 ; i < 6 ; i++)
{
// Given the str length should be less than 10
// to also store the null terminator
scanf("%s",str[i]) ;
}
printf("\n") ;
for(i = 0 ; i < 6 ; i++)
{
printf("%s",str[i]) ;
printf("\n") ;
}
return 0 ;
}

Resources