From 30b42a0a5ae60a7db12fd6d1a3158b3c0e2b734f Mon Sep 17 00:00:00 2001 From: Riya <76547581+RR-08@users.noreply.github.com> Date: Sun, 30 Oct 2022 16:16:03 +0530 Subject: [PATCH 01/10] Strings --- 12-Strings/Initializing-Strings.c | 67 ++++++++++++++++++++++++ 12-Strings/Passing-Strings-to-function.c | 15 ++++++ 12-Strings/Reading-&-modifying-strings.c | 15 ++++++ 3 files changed, 97 insertions(+) create mode 100644 12-Strings/Initializing-Strings.c create mode 100644 12-Strings/Passing-Strings-to-function.c create mode 100644 12-Strings/Reading-&-modifying-strings.c diff --git a/12-Strings/Initializing-Strings.c b/12-Strings/Initializing-Strings.c new file mode 100644 index 0000000..1ce9407 --- /dev/null +++ b/12-Strings/Initializing-Strings.c @@ -0,0 +1,67 @@ +/* +------------------------------------------------------------------------------------ +Strings are used for storing text/characters. + +In C, you must use the char type and create an array of characters to make a string. + +Note:You have to use double quotes (""). + +Methods for creating/intializing strings in C + +I) Assigning a string literal without size: + + char str[] = "Code"; + +II) Assigning a string literal with a predefined size + + char str[5] = "Code"; + + Note: always declare a string with a size equal to or greater than n+1. + +III) Assigning character by character with size: + + char str[14] = { 'C','o','d','e','\0'}; + + Note:remember to set the end character as ‘\0’ which is a null character. + +IV) Assigning character by character without size: + + char str[] = { 'C','o','d','e','\0'}; + + +Note: Arrays and strings are second-class citizens in C; they do not support the assignment operator once it is declared. + char c[100]; + c = "C programming"; // Error! array type is not assignable. +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide + +#include // Header file that includes Strlen function to find length of string +#include + +int main() +{ + int len1, len2, len_2, len3, len4; // Intializing lengths to check the length of string + + char str1[]="Code"; // Initializing string using Method I + printf("%s\n", str1); + len1 = strlen(str1); + + printf("Length of %s is %d", str1, len1); // Lenth would be 4 + + char str2[5]="Code"; // Intializing string using Method II + char str_2[4]="Code"; + printf("%s\t%s\n", str2, str_2); + len2 = strlen(str2); + len_2 = strlen(str_2); // This might give incorrect value of length as the array size is less than (actual length + 1) of the string + + printf("Length of %s is %d", str2, len2); +} +/* +------------------------------------------------------------------------------------ + Challenge: Initialize a string "Inspire Zone" using different methods and find out the first and last occurence of 'n' in the string. + + Hint: look for string functions in C +------------------------------------------------------------------------------------ +*/ diff --git a/12-Strings/Passing-Strings-to-function.c b/12-Strings/Passing-Strings-to-function.c new file mode 100644 index 0000000..05270af --- /dev/null +++ b/12-Strings/Passing-Strings-to-function.c @@ -0,0 +1,15 @@ +/* +------------------------------------------------------------------------------------ + +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide +int main() +{ +} +/* +------------------------------------------------------------------------------------ + Challenge: list challenges to be completed here. minimum of one challenge per tutorial +------------------------------------------------------------------------------------ +*/ diff --git a/12-Strings/Reading-&-modifying-strings.c b/12-Strings/Reading-&-modifying-strings.c new file mode 100644 index 0000000..05270af --- /dev/null +++ b/12-Strings/Reading-&-modifying-strings.c @@ -0,0 +1,15 @@ +/* +------------------------------------------------------------------------------------ + +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide +int main() +{ +} +/* +------------------------------------------------------------------------------------ + Challenge: list challenges to be completed here. minimum of one challenge per tutorial +------------------------------------------------------------------------------------ +*/ From 9fcad074645cd5dbab460ff322466c215eb208c3 Mon Sep 17 00:00:00 2001 From: Fum <66768334+funbeedev@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:00:12 +0000 Subject: [PATCH 02/10] Delete empty file --- 12-Strings/Passing-Strings-to-function.c | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 12-Strings/Passing-Strings-to-function.c diff --git a/12-Strings/Passing-Strings-to-function.c b/12-Strings/Passing-Strings-to-function.c deleted file mode 100644 index 05270af..0000000 --- a/12-Strings/Passing-Strings-to-function.c +++ /dev/null @@ -1,15 +0,0 @@ -/* ------------------------------------------------------------------------------------- - ------------------------------------------------------------------------------------- -*/ - -// Code here explaining concept with comments to guide -int main() -{ -} -/* ------------------------------------------------------------------------------------- - Challenge: list challenges to be completed here. minimum of one challenge per tutorial ------------------------------------------------------------------------------------- -*/ From 7d1e473dac8d9955392dc51981734de48a208b36 Mon Sep 17 00:00:00 2001 From: Fum <66768334+funbeedev@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:00:23 +0000 Subject: [PATCH 03/10] Delete empty file --- 12-Strings/Reading-&-modifying-strings.c | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 12-Strings/Reading-&-modifying-strings.c diff --git a/12-Strings/Reading-&-modifying-strings.c b/12-Strings/Reading-&-modifying-strings.c deleted file mode 100644 index 05270af..0000000 --- a/12-Strings/Reading-&-modifying-strings.c +++ /dev/null @@ -1,15 +0,0 @@ -/* ------------------------------------------------------------------------------------- - ------------------------------------------------------------------------------------- -*/ - -// Code here explaining concept with comments to guide -int main() -{ -} -/* ------------------------------------------------------------------------------------- - Challenge: list challenges to be completed here. minimum of one challenge per tutorial ------------------------------------------------------------------------------------- -*/ From b453853ce22fd5b26c941ad8132d34dcefa81c7a Mon Sep 17 00:00:00 2001 From: Fum <66768334+funbeedev@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:40:54 +0000 Subject: [PATCH 04/10] Tidy up structure of tutorial --- 12-Strings/Initializing-Strings.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/12-Strings/Initializing-Strings.c b/12-Strings/Initializing-Strings.c index 1ce9407..7fb1e3c 100644 --- a/12-Strings/Initializing-Strings.c +++ b/12-Strings/Initializing-Strings.c @@ -35,28 +35,25 @@ Note: Arrays and strings are second-class citizens in C; they do not support the ------------------------------------------------------------------------------------ */ -// Code here explaining concept with comments to guide - #include // Header file that includes Strlen function to find length of string #include int main() { - int len1, len2, len_2, len3, len4; // Intializing lengths to check the length of string + int len1, len2, len3; // Intializing lengths to check the length of string char str1[]="Code"; // Initializing string using Method I - printf("%s\n", str1); len1 = strlen(str1); - - printf("Length of %s is %d", str1, len1); // Lenth would be 4 + printf("\n Length of %s is %d", str1, len1); // Lenth would be 4 char str2[5]="Code"; // Intializing string using Method II - char str_2[4]="Code"; - printf("%s\t%s\n", str2, str_2); len2 = strlen(str2); - len_2 = strlen(str_2); // This might give incorrect value of length as the array size is less than (actual length + 1) of the string + printf("\n Length of %s is %d", str2, len2); + + char str3[4]="Code"; + len3 = strlen(str3); // This might give incorrect value of length as the array size is less than (actual length + 1) of the string + printf("\n Length of %s is %d", str3, len3); - printf("Length of %s is %d", str2, len2); } /* ------------------------------------------------------------------------------------ From 50836b40c8f80fd44b208d33110b2c017355bd46 Mon Sep 17 00:00:00 2001 From: Fum <66768334+funbeedev@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:42:28 +0000 Subject: [PATCH 05/10] correct linting --- 12-Strings/Initializing-Strings.c | 1 - 1 file changed, 1 deletion(-) diff --git a/12-Strings/Initializing-Strings.c b/12-Strings/Initializing-Strings.c index 7fb1e3c..2b15fe5 100644 --- a/12-Strings/Initializing-Strings.c +++ b/12-Strings/Initializing-Strings.c @@ -53,7 +53,6 @@ int main() char str3[4]="Code"; len3 = strlen(str3); // This might give incorrect value of length as the array size is less than (actual length + 1) of the string printf("\n Length of %s is %d", str3, len3); - } /* ------------------------------------------------------------------------------------ From 63b7eed8877ce5770acaef17ec90b1223ca51356 Mon Sep 17 00:00:00 2001 From: Fum <66768334+funbeedev@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:44:11 +0000 Subject: [PATCH 06/10] Rename Initializing-Strings.c to initializing-strings.c --- 12-Strings/{Initializing-Strings.c => initializing-strings.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 12-Strings/{Initializing-Strings.c => initializing-strings.c} (100%) diff --git a/12-Strings/Initializing-Strings.c b/12-Strings/initializing-strings.c similarity index 100% rename from 12-Strings/Initializing-Strings.c rename to 12-Strings/initializing-strings.c From 3c8cdf243b4d0b44f982f0e3d2d85c67de73831c Mon Sep 17 00:00:00 2001 From: Riya <76547581+RR-08@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:21:56 +0530 Subject: [PATCH 07/10] reading-string --- 12-Strings/reading-string.c | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 12-Strings/reading-string.c diff --git a/12-Strings/reading-string.c b/12-Strings/reading-string.c new file mode 100644 index 0000000..92dd52c --- /dev/null +++ b/12-Strings/reading-string.c @@ -0,0 +1,43 @@ +/* +------------------------------------------------------------------------------------ +There are various functions to read input string from users. + + I) The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.). + + Note: Also notice that we have used the code input_string_variable_name instead of ' &input_string_variable_name ' with scanf(). + eg: scanf("%s", name); + This is because name is a char array, and we know that array names decay to pointers in C.Thus, the name in scanf() already points to the address of the first element in the string, which is why we don't need to use &. + + II) To read entire line fgets() function is used. +fgets(name, sizeof(name), stdlin); // read string + +Note: The gets() function can also be to take input from the user. However, it is removed from the C standard. + +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide +#include +int main() +{ + char name[30]; + printf("Enter full name: "); + fgets(name, sizeof(name), stdin); // read string + printf("You entered the following name: "); + puts(name); // display string + return 0; +} +/* +------------------------------------------------------------------------------------ + Challenge: Develope a program to accept user name and a an integer value indicating their number of PRs made in Hacktoberfest and display the message as follows + + 1. If no of PRs>=4: + + Hello , You have successfully completed Hacktoberfest. + + 2. In all other cases: + + Hello , + You need minimum 4 PRs to complete the Hacktoberfest Challenge. +------------------------------------------------------------------------------------ +*/ From 5912227f8e17bdae61b8d848877581a38f3e645a Mon Sep 17 00:00:00 2001 From: Riya <76547581+RR-08@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:26:40 +0530 Subject: [PATCH 08/10] passing-string-to-function --- 12-Strings/passing-strings-to-function.c | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 12-Strings/passing-strings-to-function.c diff --git a/12-Strings/passing-strings-to-function.c b/12-Strings/passing-strings-to-function.c new file mode 100644 index 0000000..78b5426 --- /dev/null +++ b/12-Strings/passing-strings-to-function.c @@ -0,0 +1,50 @@ +/* +------------------------------------------------------------------------------------ + Strings can be passed to a function in a similar way as arrays. + + To pass a one dimensional string to a function as an argument we just write the name of the string array variable. + + In the following example we have a string array variable str and it is passed to the function. + + Method 1: Pass by value / without using pointers + + void function(char str[]); + char str[50]; + function(str); // Passing string to a function. + + Method 2: Using Pointers + + void function(char* str); + char str[50]; + function(str); // Passing string to a function. +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide +#include +void sayWelcome(char str[]); // Passing string as reference +void sayGoodbye(char* str); // Passing string as a pointer variable + +int main() +{ + char str[50]; + printf("Enter string: "); + fgets(str, sizeof(str), stdin); + sayWelcome(str); // Passing string to function + sayGoodbye(str); + return 0; +} +void sayWelcome(char str[]) +{ + printf("Welcome, "); + puts(str); +} +void sayGoodbye(char* str){ + printf("Good Bye, "); + puts(str); +} +/* +------------------------------------------------------------------------------------ + Challenge: Create a user defined function which accepts string as argument and counts the total number of character in the inputed string. +------------------------------------------------------------------------------------ +*/ From 61b22f06d91f524c6c8c61ca3484496d1361dfef Mon Sep 17 00:00:00 2001 From: Riya <76547581+RR-08@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:40:14 +0530 Subject: [PATCH 09/10] Remove passing-string-to-func --- 12-Strings/passing-strings-to-function.c | 50 ------------------------ 1 file changed, 50 deletions(-) delete mode 100644 12-Strings/passing-strings-to-function.c diff --git a/12-Strings/passing-strings-to-function.c b/12-Strings/passing-strings-to-function.c deleted file mode 100644 index 78b5426..0000000 --- a/12-Strings/passing-strings-to-function.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ------------------------------------------------------------------------------------- - Strings can be passed to a function in a similar way as arrays. - - To pass a one dimensional string to a function as an argument we just write the name of the string array variable. - - In the following example we have a string array variable str and it is passed to the function. - - Method 1: Pass by value / without using pointers - - void function(char str[]); - char str[50]; - function(str); // Passing string to a function. - - Method 2: Using Pointers - - void function(char* str); - char str[50]; - function(str); // Passing string to a function. ------------------------------------------------------------------------------------- -*/ - -// Code here explaining concept with comments to guide -#include -void sayWelcome(char str[]); // Passing string as reference -void sayGoodbye(char* str); // Passing string as a pointer variable - -int main() -{ - char str[50]; - printf("Enter string: "); - fgets(str, sizeof(str), stdin); - sayWelcome(str); // Passing string to function - sayGoodbye(str); - return 0; -} -void sayWelcome(char str[]) -{ - printf("Welcome, "); - puts(str); -} -void sayGoodbye(char* str){ - printf("Good Bye, "); - puts(str); -} -/* ------------------------------------------------------------------------------------- - Challenge: Create a user defined function which accepts string as argument and counts the total number of character in the inputed string. ------------------------------------------------------------------------------------- -*/ From b1591c857d47a6fd0f806bf8819a93ad2d51beb2 Mon Sep 17 00:00:00 2001 From: Riya <76547581+RR-08@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:46:48 +0530 Subject: [PATCH 10/10] passing-string-to-function --- 12-Strings/passing-strings-to-function.c | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 12-Strings/passing-strings-to-function.c diff --git a/12-Strings/passing-strings-to-function.c b/12-Strings/passing-strings-to-function.c new file mode 100644 index 0000000..78b5426 --- /dev/null +++ b/12-Strings/passing-strings-to-function.c @@ -0,0 +1,50 @@ +/* +------------------------------------------------------------------------------------ + Strings can be passed to a function in a similar way as arrays. + + To pass a one dimensional string to a function as an argument we just write the name of the string array variable. + + In the following example we have a string array variable str and it is passed to the function. + + Method 1: Pass by value / without using pointers + + void function(char str[]); + char str[50]; + function(str); // Passing string to a function. + + Method 2: Using Pointers + + void function(char* str); + char str[50]; + function(str); // Passing string to a function. +------------------------------------------------------------------------------------ +*/ + +// Code here explaining concept with comments to guide +#include +void sayWelcome(char str[]); // Passing string as reference +void sayGoodbye(char* str); // Passing string as a pointer variable + +int main() +{ + char str[50]; + printf("Enter string: "); + fgets(str, sizeof(str), stdin); + sayWelcome(str); // Passing string to function + sayGoodbye(str); + return 0; +} +void sayWelcome(char str[]) +{ + printf("Welcome, "); + puts(str); +} +void sayGoodbye(char* str){ + printf("Good Bye, "); + puts(str); +} +/* +------------------------------------------------------------------------------------ + Challenge: Create a user defined function which accepts string as argument and counts the total number of character in the inputed string. +------------------------------------------------------------------------------------ +*/