Skip to content

Commit

Permalink
Merge pull request #1 from RR-08/RR-08-patch-1
Browse files Browse the repository at this point in the history
passing-string-to-function
  • Loading branch information
RR-08 authored Oct 31, 2022
2 parents 61b22f0 + b1591c8 commit bcba385
Showing 1 changed file with 50 additions and 0 deletions.
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.
------------------------------------------------------------------------------------
*/

0 comments on commit bcba385

Please sign in to comment.