Strings and Command Line Arguments in Pragramming Language C
Strings and Command Line Arguments in Pragramming 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'.
– 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
#include <string.h>
int main()
/* String Declaration*/
char name[20];
scanf("%s", name);
/*Displaying String*/
printf("%s",name);
return 0;
Output:
San
#include <string.h>
int main()
/* String Declaration*/
char name[20];
gets(name);
puts(name);
return 0;
Output:
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.
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 str3[12];
int len ;
strcpy(str3, str1);
len = strlen(str1);
return 0;
output−
strlen(str1) : 10
Example of strcmp function:
#include <stdio.h>
#include <string.h>
int main()
else
return 0;
Output:
#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
#include <stdio.h>
#include <string.h>
int main()
return 0;
Output:
• 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.
• Syntax:
Here argc is an ARGument Count that counts the number of arguments on the command
line
– They are used to control program from outside instead of hard coding those
values inside the code.
– argv[1] points to the first command line argument and argv[n] points last
argument.
Example:
#include <stdio.h>
if( argc == 2 )
else
When the above code is compiled and executed with single argument, it produces the
following result.
$./a.out testing
When the above code is compiled and executed without passing any argument, it
produces the following result.
$./a.out
When the above code is compiled and executed with a single argument separated by
space but inside double quotes, it produces the following result.
• 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.