I have an array of strings in C:
char taula[15][15];
This table is filled with the sentences of a file. The maximum letters a word can have is 15. The maximum words a phrase can have is also 15. This is its content:
{ "may", "the", "force", "be", "with", "you." }
Now, I need to change the order of the words so they are not in this order (but that doesn't matter for the question).
I am new to C, and what my brain has though is this (this is what I would do in Java):
char aux[15] = "";
char frase_nova[15][15];
for(i=0;i<15;i=i 2) {
aux = taula[i];
taula[i] = taula[i 1];
i ;
}
I create an aux variable to store a word, and create a new array to store the disordered phrase. I store the 1st element to the aux variable. Assign the 0th element to the 1st, and finally I assign the aux to the 0th element, and so on. This way I should get this phrase as result:
the may be force you. with
This has caused me an error, so I searched why, and I changed that code to this code, using strcpy:
strcpy(aux,frase_nova[i 1]);
strcpy(frase_nova[i 1],frase_nova[i]);
strcpy(frase_nova[i],aux);
printf("%s",frase_nova[i]);
But this causes me a strange result. Something that I don't understand is happening with the pointers and the arrays or whatever it is.
the E�Ube you.��aE�URE�U
So I would like to know how to fill a new multidimensional char array with values of an original multidimensional char array in the order ([i]) I want.
CodePudding user response:
The maximum letters a word can have is 15.
Then you should have size of words, in 2D char array, as 16 and not 15
char taula[15][16];
^^
because, in C, strings are actually one-dimensional array of characters terminated by a null character '\0'. Hence, you need space to accommodate null character '\0'.
Your array has 6 strings i.e. 3 pairs, so you need iterate loop only 3 times. But, looks at this for loop:
for(i=0;i<15;i=i 2) {
it will iterate for more than 3 times. May you can keep track of number of elements in your 2D array and use it instead of 15 in the loop condition.
As you understood that these statements, of for loop body, won't work:
aux = taula[i];
taula[i] = taula[i 1];
because both aux and taula are arrays. But there is one more problem in your loop body and which is this statement:
i ;
You are already incrementing i in loop - i=i 2 and if you do i in loop body then, in every iteration, i will be incremented by 3 and which will not give the desired results. You don't need to do i in loop body.
There is no problem with these statements:
strcpy(aux,frase_nova[i 1]);
strcpy(frase_nova[i 1],frase_nova[i]);
strcpy(frase_nova[i],aux);
printf("%s",frase_nova[i]);
provided
- if the array
frase_novahas elements{ "may", "the", "force", "be", "with", "you." }, and. - loop variable
iis incremented twice only in every iteration.
Check the following code:
#include <stdio.h>
#include <string.h>
#define WORDS 15
#define WORD_SZ 16
#define NUM_ELE 6
int main(void) {
char frase_nova[WORDS][WORD_SZ] = { "may", "the", "force", "be", "with", "you." };
char aux[WORD_SZ] = "";
for(int i = 0; i < NUM_ELE; i = i 2) {
// using your posted statements as it is
strcpy(aux, frase_nova[i 1]);
strcpy(frase_nova[i 1], frase_nova[i]);
strcpy(frase_nova[i], aux);
printf("%s ", frase_nova[i]);
}
printf ("\n");
for (int i = 0; i < NUM_ELE; i) {
printf ("%s ", frase_nova[i]);
}
printf ("\n");
return 0;
}
Output:
# ./a.out
the be you.
the may be force you. with
So I would like to know how to fill a new multidimensional char array with values of an original multidimensional char array
This is not clear whether you want to fill the new multidimensional char array with the values of original multidimensional char array before swapping the strings of original array or after swapping the strings. But, whatever you want, you can simply achieve it by doing this - strcpy(frase_nova[i], taula[i]).
CodePudding user response:
To swap 6 words you need to iterate 6 times, not 15 times. Unless you simply wish to shuffle words around according to some strange pattern.
Also the loop doesn't make any sense. I don't understand why you do i=i 2. Also any loop of the nature for(int i=0; i<n; i ) can never access item array[i 1] in the loop body, because that leads to out of bounds access.
