Srting in C
STRING
Q.1 What is string?
Ans: In c language, a string is nothing but a null
terminated character array. This means that after the last character a null character
(‘\0’) is used to declare the end of the character array. For example we write,
Char str[] =
“HELLO”;
We are declaring a character array that has five usable
characters namely, H, E, L, L and O. A null character (‘\n’) is also stored
after those 5 words. So the internal representation of the string needs 5 + 1 locations
(1 extra for null character).
If we had declared str as,
Char str[5] =
“HELLO”;
Then the null character will not be appended automatically
to the character array.Because in this case the str can hold only 5 characters
and the character in HELLO have already filled the space allocated to it.
Function used in string in c :
1.Strcpy function:
Since C never lets us
assign entire arrays, we use the strcpy function to copy one string to another:
#include <string.h>
char string1[] =
"Hello, world!";
char string2[20];
strcpy(string2,
string1);
The destination string
is strcpy's first argument, so that a call to strcpy mimics an
assignment expression (with the destination on the left-hand side). Notice that
we had to allocate string2 big enough to hold the string that would
be copied to it. Also, at the top of any source file where we're using the
standard library's string-handling functions (such as strcpy) we must include the
line
#include
<string.h>
which contains external
declarations for these functions.
2.Strcmp Function :
Since C won't let us
compare entire arrays, either, we must call a function to do that, too. The
standard library's strcmp function compares two strings, and returns
0 if they are identical, or a negative number if the first string is
alphabetically ``less than'' the second string, or a positive number if the
first string is ``greater.'' (Roughly speaking, what it means for one string to
be ``less than'' another is that it would come first in a dictionary or
telephone book, although there are a few anomalies.) Here is an example:
char string3[] = "this is";
char string4[] =
"a test";
if(strcmp(string3,
string4) == 0)
printf("strings
are equal\n");
else printf("strings are
different\n");
This code fragment will
print ``strings are different''. Notice that strcmp does not return
a Boolean, true/false, zero/nonzero answer, so it's not a good idea to write
something like
if(strcmp(string3,
string4))
...
because
it will behave backwards from what you might reasonably expect. (Nevertheless,
if you start reading other people's code, you're likely to come across
conditionals like if(strcmp(a,
b)) or evenif(!strcmp(a, b)). The first does something if the strings are
unequal; the second does something if they're equal.
3.Strcat function :
Another
standard library function is strcat, which concatenates strings. It does not concatenate
two strings together and give you a third, new string; what it really does is
append one string onto the end of another. (If it gave you a new string, it
would have to allocate memory for it somewhere, and the standard library string
functions generally never do that for you automatically.) Here's an example:
char string5[20] =
"Hello, ";
char string6[] =
"world!";
printf("%s\n",
string5);
strcat(string5,
string6);
printf("%s\n",
string5);
The first call
to printf prints ``Hello, '', and the second one prints ``Hello,
world!'', indicating that the contents of string6 have been tacked on
to the end of string5. Notice that we declared string5 with
extra space, to make room for the appended characters.
4.strlen function :
If you have a string and
you want to know its length (perhaps so that you can check whether it will fit
in some other array you've allocated for it), you can call strlen, which returns the
length of the string (i.e. the number of characters in it), not including
the \0:
char string7[] = "abc";
int len =
strlen(string7);
printf("%d\n",
len);
5. strncat function :
Syntax:
Char *strncat(char *str1,const char *str2,size_t n);
Appends the string pointed to by
str2 to the end of the strings pointed to by str1 up to n characters long. The
terminating null character of str1 is overwritten. Copying stops when n
character are copied or the terminating
null character of str2 is copied. A terminating null character is appended to
str1 before returning to the calling function.
#include<stdio.h>
#include<string.h>
int main()
{
Char str1[50] = “Programming”;
Char str2[] = “In C”
Strncat(str1,str2,2);
Printf(“\n str1:%s”,str1);
Return 0;
}
Output
Str1:Programming In
Strchr function:
This function searches for the first occurrence of the
character c in the string pointed to by the argument str. The function returns a
pointer pointing to the first matching character, or null if no match was
found.
#include<stdio.h>
#include<string.h>
int main()
{
Char str[50] = “Programming In C”;
Char *pos;
pos= strchr(str, ‘n’)
if (pos)
Printf(“\n n is found in str at position %d”, pos);
else
printf(“\n n is not found in the string”);
Return 0;
}
Output:
n is found in str at position 9
The strrchr
function :
This function searches for the first
occurrence of the character c beginning at the rear end and working towards the
front in the string pointed to by the
argument str. The function returns a pointer pointing to the first matching
character, or null if no match was found.
#include<stdio.h>
#include<string.h>
int main()
{
Char str[50] = “Programming In C”;
Char *pos;
pos= strrchr(str, ‘n’)
if (pos)
Printf(“\n The last position of n is %d”, pos-str);
else
printf(“\n n is not found in the string”);
Return 0;
}
Output:
The last position of n is 13
No comments