#include #include int main() { char cars[][10] = {"Mustang","Corvette","Camaro"}; //cars[0] = "Tesla"; strcpy(cars[0], "Tesla"); for(int i = 0; i < sizeof(cars)/sizeof(cars[0]); i++) { printf("%s ", cars[i]); } return 0; }
@sanaolsadako34172 жыл бұрын
How can I insert another string in that array?
@markangelcalaycay4165 Жыл бұрын
2:54 2:54 2:54 2:54 2:54
@alexiosalexiou31802 жыл бұрын
Thanks, simple, straight to the point, analytical.
@benoitgauthier608911 ай бұрын
One VERY important point when using strcpy() that really should have been mentionned: The destination string should be large enough to accept the source string. Otherwise, it will overflow on other memory adresses and potentially corrupt your program
@Aymanne-io6vb7 ай бұрын
You can also put space to occupy the string space being replaced if the string to replace another string is not large enough, not a permanent solution though: #include #include int main(){ char names[][10] = {"Hello", "Cameroon", "Doremi", "colagge"}; printf("No. of rows: %d ", sizeof(names)/sizeof(names[0] ) ) ; printf("No. of cols: %d ", sizeof(names)/sizeof(names[0] ) ) ; strcpy(names[1], "Dora " ); //Changes value of a string array. Put space to occupy the rest of the letters since the initial string is large. for(int i = 0; i < sizeof(names)/sizeof(names[0]) ; i++){ printf(" %s ", names[i]); } /* for(int i = 0; i < sizeof(names)/sizeof(names[0]); i++){ for(int j = 0; j < sizeof(names[0])/sizeof(names[0][0]); j++){ printf("%c", names[i][j]); } printf(" "); } */ return 0; }
@mohammedazzan75293 жыл бұрын
yours was the simplest to understand thank you.
@farragoprismproductions333729 күн бұрын
For those who are dumb like me, the reason why he used two brackets for assigning an "array of strings" is because of *"an array of characters and the array of strings composed of those characters"* . A *string* in the _C language_ is simply *_a list of characters_* , but C prefers to associate it with the object known as an *array* , among other programming languages. So, having a list of words will usually entail creating a variable that stores two types of arrays, therefore it's important to use two brackets for what would then be a *2-dimensional array* . *_Definitions -_* *1. **_2-dimensional Array:_* [The incorporation of an array/list of elements that would intuitively involve the combination of two arrays.] *2. **_Arrays:_* [ 1.) A list of elements that can be manipulated in a coding project, assigned with *at least* a single bracket. 2.) A type of object in the C programming language.] *3. **_String:_* [A type of array that consists of characters.] *4. **_Characters:_* [ 1.) A data type in the C programming language, which represents a letter in the alphabet. 2.) A type of element that can be used in an array.] *5. **_Element:_* [A value that is stored in an array] *6. **_Bracket/Index Notation:_* [A notation that's used for indexing elements, but is used for limiting the number of possible elements within an array.]
@kevser176 Жыл бұрын
ı really couldnt get this topic but now everything is clear. thanks you are the best🥺
@EasyMathematics01Ай бұрын
0:27 hello Bro, i am watching from Nepal. Mustang district is located in my country which is very popular for tourism and Himalayas. anyway thank u so much for your best teaching style sir. ❤❤❤
@movieyouser Жыл бұрын
Sir, you are the help I needed
@marbles55902 жыл бұрын
Thank you for this video! It can also be *cars[ ] in order to not have wastage of memory
@minari_03242 жыл бұрын
damn you made our 30-60 min lecture into 3 mins
@sebastienroux17902 жыл бұрын
Can you replace for example "tesla" with a variable that ="tesla" ?
I'm pretty late but i think no since a string itself is an array of characters so an array of strings must be a type of 2D array
@zraie2455 Жыл бұрын
A string is a 1D array of characters
@ambeikaam36094 ай бұрын
yepp for example: #include #define MAX_LEN 10 int main(void) { char lecturer_name[MAX_LEN] = {'A','n','g','e','l','a' }; char lecturer_name[MAX_LEN] = "Angela"; //this is a string char word[MAX_LEN] = {'h', 'e', 'l', 'l', 'o', '\0'}; int i = 0; while (i < MAX_LEN) { printf("%c", lecturer_name[i]); i++; } printf(" "); return 0; }
@farragoprismproductions333729 күн бұрын
@@ambeikaam3609 lol You literally helped me turn this: #include int main(void) { const int NUM_PLAYERS = 8; // Number of elements int userVals[NUM_PLAYERS]; // User values int i; for (i = 0; i < NUM_PLAYERS; ++i) { printf("Enter player %d's jeresy number:", i + 1); scanf("%d", &userVals[i]); } return 0; } *into this:* #include #define NUM_PLAYERS 8 *
@kyte81312 жыл бұрын
and how the hell do i take input from user?
@zkskakksis1514 Жыл бұрын
Scanf
@shouryadeepbera7114 Жыл бұрын
@@zkskakksis1514causes problem when you take consecutive inputs using a loop.
@zkskakksis1514 Жыл бұрын
@@shouryadeepbera7114what do you mean?
@shouryadeepbera7114 Жыл бұрын
@@zkskakksis1514 try taking inputs in a string array using loop, it will cause errors...which can be fixed by clearing the input buffer after each input.
@zraie2455 Жыл бұрын
@@shouryadeepbera7114 #include #include #define NAME_LIST_SIZE 10 #define MAX_STR_LEN 21 int main() { char name_list[NAME_LIST_SIZE][MAX_STR_LEN]; printf("Please enter a name: "); char user_input[MAX_STR_LEN]; int i = 0; while(i < NAME_LIST_SIZE && fgets(user_input, MAX_STR_LEN, stdin) != NULL) { strncpy(name_list[i], user_input, strlen(user_input) - 1); printf("%s ", name_list[i]); i++; printf("Please enter a name: "); } printf(" Program finish ^_^"); return 0; } It's better to use fgets in that case
@moaz_king_1001 Жыл бұрын
thanks for help :)
@bernardev32 жыл бұрын
THANKS BRO!!!!
@arkabrian57963 жыл бұрын
THANK YOU SO MUCH!!!
@TheLivingCrittles6 ай бұрын
I dont understand the math for the for loop... sizeof(cars) would be 2, since 0 counts as an index. sizeof(cars[0]) is Tesla, which is 5 characters... so wouldn't you be comparing 0, 1, and 2 against 2/5 which is 0.4? so shouldn't the loop break after the first element? so confused...
@umutaydn6184 Жыл бұрын
thanks alot!
@potatopoo69783 жыл бұрын
Why is mustang not printed?
@MRCN492 жыл бұрын
we changed it to "Tesla" a line below
@roronoazoro56722 ай бұрын
can someone tell why did it not print mustang?
@Wintre_Hesmith2 ай бұрын
The 9th line of code, strcpy replaced the first string element "Mustang" with "Tesla" instead
@roronoazoro56722 ай бұрын
@@Wintre_Hesmith alr thanks bud
@stonedcodingtom90972 жыл бұрын
Thx Bro
@vlastimirzdravkovic76242 жыл бұрын
I fucking love you
@latesttrailers-yb8pr Жыл бұрын
Mine ain't working
@MostaphaQamar Жыл бұрын
dark theme sucks. view is not clear. use light color