Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing-string-to-function #100

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
50 changes: 50 additions & 0 deletions 12-Strings/passing-strings-to-function.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
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.
------------------------------------------------------------------------------------
*/
43 changes: 43 additions & 0 deletions 12-Strings/reading-string.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
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 <user's name>, You have successfully completed Hacktoberfest.

2. In all other cases:

Hello <user's name>,
You need minimum 4 PRs to complete the Hacktoberfest Challenge.
------------------------------------------------------------------------------------
*/