Strings
string is a sequence
of characters terminated with a null character \0.
The C compiler
automatically places the '\0' at the end of the string when it initializes the
array
Eg.
char c [] = "c
string";
char c[] =
"abcd";
char c[50] =
"abcd";
char c[] = {'a', 'b',
'c', 'd', '\0'};
char c[5] = {'a',
'b', 'c', 'd', '\0'};
Read
String from the user
The scanf() function
reads the sequence of characters until it encounters whitespace (space,
newline, tab, etc.)
Eg.
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
gets() and puts()-Functions gets() and puts() are two string functions to take string input from the user and display it . fgets() function to read a line of string. And, puts() to display the string.
{
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
fgets(name,
sizeof(name), stdlin); // read string
The sizeof(name) results
to 30. Hence, we can take a maximum of 30 characters as input which is the size
of the name string.
To print the string,
we have used puts (name);
String Functions
Function |
Work of Function |
computes
string's length |
|
copies
a string to another |
|
concatenates(joins)
two strings |
|
compares
two strings |
|
strlwr() |
converts
string to lowercase |
strupr() |
converts
string to uppercase |
The following example
uses some of the above-mentioned functions –
#include <stdio.h>
#include <string.h>
{
char str2[12] = "World";
char str3[12];
int
len ;
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
}
Strings
and Pointers
we can use pointers
to manipulate elements of the string.
{
char name[] = "Harry Potter";
printf("%c", *(name+1)); // Output: a
printf("%c", *(name+7)); // Output: o
printf("%c", *namePtr); // Output: H
printf("%c", *(namePtr+1)); // Output: a
printf("%c", *(namePtr+7)); // Output: o
}
Array of String
An
array itself defines as a list of strings. The string is a collection of
characters, an array of a string is an array of arrays of characters. An
array of a string is one of the most common applications of two-dimensional
arrays.
datatypename_of_the_array[size_of_elements_in_arra];
examples
char str_name[size];
char str_name[8] = "Strings";
char str_arr[2][6] ={"gouri",
"ram"};
char str_name[8] = {'s','t','r','i','n','g','s','\0'};
Examples of Array
String
#include
<stdio.h>
int main()
{
char name[10];
printf("Enter
the name: ");
fgets(name, sizeof(name), stdin);
printf("Name
is : ");
puts(name);
return 0;
}
Command Line Arguments
Command-line arguments are simple
parameters that are given on the system's command line, and the values of these
arguments are passed on to your program during program execution.
- When the main function of a
program contains arguments, then these arguments are known as Command Line
Arguments.
- The main function can be
created with two methods: first with no parameters (void) and second with
two parameters.
- The parameters are argc and
argv, where argc is an integer and the argv is a list of command line
arguments.
- argc denotes the number
of arguments given, while argv[] is a pointer array pointing to
each parameter passed to the program.
- If no argument is given, the
value of argc will be 1.
- The value of argc should
be non-negative.
- argc
(ARGument Count) denotes the number of arguments to be passed and
- argv
[ ] (ARGument Vector) denotes a pointer array pointing to every
argument that has been passed to your program.
Eg.
{
printf(" \n Name of my Program %s
\t", argv[0]);
if( argc == 2 )
{
printf("\n Value given by user is:
%s \t", argv[1]);
}
else if( argc > 2 )
{
printf("\n Many values given by
users.\n");
}
else
{
printf(" \n Single value
expected.\n");
}
}
0 comments:
Post a Comment