A more convenient way to initialize a C
string is to initialize it through character array:
char str[ ] = "Join Protec"; This is same as initializing it
as follows: char str[ ] = { 'J', 'o', 'i', 'n', ' ', 'P', 'r', 'o', 't', 'e', 'c', '\0' }; It can be initialized by two ways:
- By Char Array
- By String Literal
Declaring by char Array: As we know that each character enclosed in single quotation
marks separated by a comma and string enclosed in double quotation marks. While
declaring string size of an array is not mandatory to define in square bracket,
so we can be define by either way as below:
Example 1:
· Char str [7] = {‘P’, ‘R’, ‘O’, ‘T’, ‘E’, ‘C’, ‘\0’};
OR
· Char str [ ] = {‘P’, ‘R’, ‘O’, ‘T’, ‘E’, ‘C’, ‘\0’};
Example 2:
Char str [17] = {‘L’, ‘e’, ‘t’, ‘s’, ‘ ’, ‘J’, ‘o’, ‘I’, ‘n’,
‘ ’, ‘P’, ‘r’, ‘o’, ‘t’, ‘e’, ‘c’, ‘\0’};
OR
Char str [ ] = {‘L’, ‘e’, ‘t’, ‘s’, ‘ ’, ‘J’, ‘o’, ‘I’, ‘n’,
‘ ’, ‘P’, ‘r’, ‘o’, ‘t’, ‘e’, ‘c’, ‘\0’};
Declaring by String Literal: In this type (in String Literal) of declaration NULL
character (‘\0’) will automatically be inserted at the end of the string.
Example 1:
· Char str [6] = “PROTEC”;
OR
· Char str [ ] = “PROTEC”;
Example 2:
· Char str [17] = “Lets Join PROTEC”
OR
· Char str [ ] = “Lets Join PROTEC”;
The length of a string is determined by a
terminating null character: '\0’. So, a string with the
contents, say, "abc" has four characters: 'a' , 'b' , 'c' ,
and the terminating null ( '\0' ) character.
For example, the word "Protec" and the
phrase "Lets Join Protec" both are strings.
Memory Representation of String:
In the below example, in the
sentence “Lets Join Protec” there are two spaces and one null character which
also considered as character. That's how the total length would be 17 characters.
And in word “PROTEC” has 7 characters.
I will update related example soon...
String I/O Function in C:
Input
functions: scanf () and gets () both
are used to reads inputs given by user. Difference between them is: scanf()
reads input until it encounters whitespace, newline or End Of
File(EOF) whereas gets() reads input until it encounters
newline or End Of File(EOF), gets() does not stop reading input
when it encounters whitespace instead it takes
whitespace as a string.
I will update related example soon..
Output functions: Printf ()
and puts () both are used to display outputs on the screen. Difference between
them is: the printf
() function is used to print both strings and variables to the screen
while the puts () function only permits you to print a string
only to your screen.
I will update related example soon..
Note:The scanf() and printf() are generic i/o functions that
they support all built-in data types such as int, char, float, long, double,
hexadecimal values as well as strings.
Read & Write String in C using Printf()
& Scanf() function:
As you know that scanf() is used for reading inputs given by
the user but it has a has a drawback which stops
reading input when it encounters whitespace. Let’s consider a scenario in the below
example.
Note: format specifier %s is used for string input/output.
I will update related example soon..
It is clear in the above output
that scanf () does not allow to read the space separated strings.
To resolve this issue,
instead of writing Scanf(“%s”, str); either we
must write like this Scanf(“%[^\n]s”, str);
which instruct the compiler to store space separated string. This way
you can allow scanf() to read string even when it encounters white spaces. OR
can be use gets() function which is also use to read the space-separated strings. Let’s consider a another scenario in the below example:
I will update related example soon..
Note: Here you can notice that
there is no need to use address of (&) operator in scanf() to store a
string since String str is an array of characters and by writing the name of array i.e. str,
indicates the base address of the array. Therefore we need not use address of
(&) operator in.
Read & Write String in C using gets() &
puts() function:
I will update related example soon..
Traversing String with Example:
C program to calculate
length of the string without using Null (‘\0’):
-
Create a function and
pass string within it.
Take a counter as length (that
will store the length of the string),
initialize with 0.
Run a loop until NULL character
is not found.
Increase the counter (length)
As NULL founds return the length.
I will update related example soon...
Use of gets:
The gets()
function enables the user to enter some characters followed by the enter key.
All the characters entered by the user get stored in a character array. The
null character is added to the array to make it a string. The gets()
allows the user to enter the space-separated strings.
fputs() fgets() function:
The fputs()
function is used to write string(array of characters) to the
file. fputs(char
str[], FILE *fp); The fgets()
function is used to read string(array of characters) from the file.
String Manipulations:
Strings handling functions are defined under "string.h" header file
strncat(): ..............................................
strcat(): Concatenates(joins) two strings.
strcpy(): Copies a string to another.
strcmp(): Compares two strings.
strlwr(): Converts string to lowercase.
strupr(): Converts string to uppercase.
strlen(): Finds out the length of the string.
strlwr(): It converts string to lowercase.
String variables:
String variables are variables that
hold zero or more characters such as letters, numbers, spaces, commas and many
more. You can't use numeric functions such as addition or subtraction on string
variables.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.