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
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; }
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...
@@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.
@@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
#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;
}
How can I insert another string in that array?
2:54 2:54 2:54 2:54 2:54
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
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;
}
Thanks, simple, straight to the point, analytical.
yours was the simplest to understand thank you.
ı really couldnt get this topic but now everything is clear. thanks you are the best🥺
Thank you for this video! It can also be *cars[ ] in order to not have wastage of memory
Sir, you are the help I needed
damn you made our 30-60 min lecture into 3 mins
Can you replace for example "tesla" with a variable that ="tesla" ?
char sub[ ] = "tesla";
printf("sub = %s
", sub);
strcpy(cars[0], sub);
Yes
Can we use use strings in 1d arrays?
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
A string is a 1D array of characters
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;
}
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...
thanks for help :)
10k subs in a day nice
ı wish ı can like this video multiple times
THANKS BRO!!!!
thanks alot!
THANK YOU SO MUCH!!!
Thx bro
and how the hell do i take input from user?
Scanf
@@zkskakksis1514causes problem when you take consecutive inputs using a loop.
@@shouryadeepbera7114what do you mean?
@@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.
@@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
Why is mustang not printed?
we changed it to "Tesla" a line below
I fucking love you
can someone tell why did it not print mustang?
The 9th line of code, strcpy replaced the first string element "Mustang" with "Tesla" instead
@@Wintre_Hesmith alr thanks bud
Mine ain't working
dark theme sucks. view is not clear. use light color
Thx Bro