0% found this document useful (0 votes)
10 views11 pages

Strings and Command Line Arguments in Pragramming Language C

The document provides an overview of strings and command line arguments in the C programming language, including how to read and write strings using various functions like printf(), scanf(), gets(), and puts(). It details string manipulation functions such as strcpy, strcat, strlen, strcmp, strchr, and strstr, along with examples of their usage. Additionally, it explains command line arguments, their properties, and how they can be utilized in a C program.

Uploaded by

sasmita8687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Strings and Command Line Arguments in Pragramming Language C

The document provides an overview of strings and command line arguments in the C programming language, including how to read and write strings using various functions like printf(), scanf(), gets(), and puts(). It details string manipulation functions such as strcpy, strcat, strlen, strcmp, strchr, and strstr, along with examples of their usage. Additionally, it explains command line arguments, their properties, and how they can be utilized in a C program.

Uploaded by

sasmita8687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

IGIT, SARANG

STRINGS AND COMMAND LINE ARGUMENTS


(PROGRAMMING LANGUAGE C)

Contents:
o Introduction to String
o Read & write Strings in C using Printf() and Scanf() functions
o Read & Write Strings in C using gets() and puts() functions
o String functions and its different types (string handling functions)
o Example each of`-
• strlen, strcat, strcpy function
• strcmp function
• strchr function
• strstr function
o Command Line Arguments and its different conditions
C – Strings
Definition: Strings are actually one-dimensional array of characters terminated by
a null character '\0'.

Declaration and Initialization : The following declaration and initialization create a


string consisting of the word "Hello".

– char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

– char greeting[] = "Hello";

– Following is the memory presentation of above example

– Actually, you do not place the null character at the end of a string constant. The C
compiler automatically places the '\0' at the end of the string when it initializes the
array

String I/O in C programming


Read & write Strings in C using Printf() and Scanf() functions :
#include <stdio.h>

#include <string.h>

int main()

/* String Declaration*/

char name[20];

printf("Enter your name:");

/* I am reading the input string and storing it in name

* Array name alone works as a base address of array so

* we can use name instead of &name here */

scanf("%s", name);

/*Displaying String*/

printf("%s",name);

return 0;

Output:

Enter your name: San

San

• Note: %s format specifier is used for strings input/output


Read & Write Strings in C using gets() and puts() functions:
#include <stdio.h>

#include <string.h>

int main()

/* String Declaration*/

char name[20];

/* Console display using puts */

puts("Enter your name:");

/*Input using gets*/

gets(name);

puts(name);

return 0;

Output:

Enter your name: San

San
C – String functions
• We will see how to compare two strings, concatenate strings, copy one string to
another & perform various string manipulation operations.

• We can perform such operations using the pre-defined functions of “string.h” header
file.

• In order to use these string functions you must include string.h file in your C
program.

Different types of String handling functions

Sr.No. Function & Purpose

1 strcpy(s1, s2);
Copies string s2 into string s1.

2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.

3 strlen(s1);
Returns the length of string s1.

4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2.

5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in
string s1.

6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string
s1.
Example of strlen, strcat, strcpy function:
#include <stdio.h>

#include <string.h>

int main ()

char str1[12] = "Hello";

char str2[12] = "World";

char str3[12];

int len ;

/* copy str1 into str3 */

strcpy(str3, str1);

printf("strcpy( str3, str1) : %s\n", str3 );

/* concatenates str1 and str2 */

strcat( str1, str2);

printf("strcat( str1, str2): %s\n", str1 );

/* total lenghth of str1 after concatenation */

len = strlen(str1);

printf("strlen(str1) : %d\n", len );

return 0;

output−

strcpy( str3, str1) : Hello

strcat( str1, str2): HelloWorld

strlen(str1) : 10
Example of strcmp function:
#include <stdio.h>

#include <string.h>

int main()

char s1[12] = "Hello";

char s2[12] = "Hello.c";

if (strcmp(s1, s2) ==0)

printf("string 1 and string 2 are equal");

else

printf("string 1 and 2 are different");

return 0;

Output:

string 1 and 2 are different


Example of strchr function

#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strchr(mystr, 'f'));
return 0;
}
Output:

f function strchr

Example of strstr function

#include <stdio.h>

#include <string.h>

int main()

char inputstr[70] = "String Function in C at BeginnersBook.COM";

printf ("Output string is: %s", strstr(inputstr, 'Begi'));

return 0;

Output:

Output string is: BeginnersBook.COM


Command Line Arguments
• It is possible to pass some values from the command line to your C programs when
they are executed. These values are called command line arguments.

• These arguments are supplied after the program name in operating system command
line and these arguments values are passed to your program at time of execution
from operating system.

• It is used to control your program from outside.

• These arguments are handled by main () function.

• Syntax:

int main(int argc, char *argv[])

Here argc is an ARGument Count that counts the number of arguments on the command
line

argv[ ] is called ARGument Vector is a pointer array which holds pointers of


type char which points to the arguments passed to the program.

• The value of argc should be non-negative.

• Properties of Command Line Arguments:

– They are passed to main() function.

– They are parameters/arguments supplied to the program when it is invoked.

– They are used to control program from outside instead of hard coding those
values inside the code.

– argv[argc] is a NULL pointer.

– argv[0] holds the name of the program.

– argv[1] points to the first command line argument and argv[n] points last
argument.
Example:
#include <stdio.h>

int main( int argc, char *argv[] )

printf("Program name %s\n", argv[0]);

if( argc == 2 )

printf("The argument supplied is %s\n", argv[1]);

else if( argc > 2 )

printf("Too many arguments supplied.\n");

else

printf("One argument expected.\n");

Outputs: (in different scenarios)

When the above code is compiled and executed with single argument, it produces the
following result.

$./a.out testing

Progranm name ./a.out

The argument supplied is testing


When the above code is compiled and executed with a two arguments, it produces the
following result.

$./a.out testing1 testing2

Progranm name ./a.out

Too many arguments supplied.

When the above code is compiled and executed without passing any argument, it
produces the following result.

$./a.out

Progranm name ./a.out

One argument expected

When the above code is compiled and executed with a single argument separated by
space but inside double quotes, it produces the following result.

$./a.out "testing1 testing2“

Progranm name ./a.out

The argument supplied is testing1 testing2

• Remember that argv[0] holds the name of the program and argv[1] points to the first
command line argument and argv[n] gives the last argument. If no argument is
supplied, argc will be 1.

You might also like