String
A string in C is actually a character array. As an individual character variable can store only one character, we need an array of characters to store strings. Thus, in C string is stored in an array of characters. Each character in a string occupies one location in an array. The null character ‘\0’ is put after the last character. This is done so that program can tell when the end of the string has been reached. For example, the string ”I Like C Programming” is stored as follows.I | L | i | k | e | C | P | r | o | g | r | a | m | m | i | n | g | \o |
Since the string has 20 characters (including space), it requires an arrayof at
least, size 21 to store it.
Thus, in C, a string is a one-dimensional array of
characters terminated a null character. The terminating null character is
important. In fact, a string not terminated by ‘\0’ is not really a string, but
merely a collection of characters.
A string
may contain any character, including special control characters, such as
\n
, \t
, \\
etc...
There is an important distinction between a string and a single character in C. The convention is that single characters are enclosed by single quotes e.g. '*' and have the type char. Strings, on the hand, are enclosed by double quotes e.g. "name" and have the type "pointer to char" (char *) or array of char.
Strings can be declared in two main ways; one of these is as an array of characters, the other is as a pointer to some pre-assigned array. Perhaps the simplest way of seeing how C stores arrays is to give an extreme example which would probably never be used in practice.
#include<stdio.h>
int main ()
{
char string[];
string[0] = 'C';
string[1] = 'P';
string[2] = 'r';
string[3] = 'o';
string[4] = 'g';
string[5] = 'r';
string[6] = 'a';
string[7] = 'm';
string[8] = 'm';
string[9] = 'i';
string[10]= 'n';
string[11] = 'g';
printf ("%s", string);
return 0;
}
This method of handling strings is
perfectly acceptable, if there is time to waste, but it is so laborious that C
provides a special initialization service for strings, which bypasses the need
to assign every single character with a new assignment!.
The other way is use a pointer
to some pre-assigned array.
#include<stdio.h>
int main ()
{
char str1[]=”I Like”;
char *str2=”C Programming”;
puts(str1);
puts(str2);
return 0;
}
strlen()
Syntax:#include < string.h>
size_t strlen( char *str );
strlen(char *str) returns the length of the null terminated string pointed to by
str. The null terminator is not counted.
This function will return number of characters in the string provided
as parameter to this function. For example if we are providing "The
world" as the string it will return 9.
That means it will take the blank spaces in between the words in the
string into count. But the null terminator is not counted. It will aslo
take special characters like @,#,$,% etc into count. consider this example str[30]="www.cprogrammingexpert.com";
It is stored like
str[0] <- 'w' str[1] <- 'w' str[2] <- 'w' str[3] <- '.' str[4] <- 'c' str[5] <- 'p' str[6] <- 'r' str[7] <- 'o' str[8] <- 'g' str[9] <- 'r' str[10] <-'a' str[11] <-'m' str[12] <-'m' str[13] <-'i' str[14] <-'n' str[15] <-'g' str[16] <- 'e' str[17] <- 'x' str[18] <- 'p' str[19] <- 'e' str[20] <- 'r' str[21] <- 't' str[22] <- '.' str[23] <- 'c' str[24] <- 'o' str[25] <- 'm' str[26] <- '\0' str[27] <- 'garbage value' str[28] <- 'garbage value' str[29] <- 'garbage value'
Here strlen(str) equals 26. null terminator is not counted. Example:
string function : strlwr()
Syntax:char * strlwr(char *str)
strlwr(str) coverts str to all lowercase.
This function will convert all the uppercase characters in the string, which is provided as parameter, into lowercase.
It does not take care whether it is the starting letter or not.
It will convert every uppercase to lowercase.For example #include<stdio.h> #include<conio.h> int main() { char str[30]="www.CProgrammingExpert.com"; clrscr(); printf("\n\nstr : %s\n\n",str); printf("strlwr(str) : %s\n",strlwr(str)); getch(); return 0; }here we declares the string str like this str[30]="www.CProgrammingExpert.com"; The string str is a character array of size 30.The character is stored like this
str[0] <- 'w' str[1] <- 'w' str[2] <- 'w' str[3] <- '.' str[4] <- 'C' str[5] <- 'P' str[6] <- 'r' str[7] <- 'o' str[8] <- 'g' str[9] <- 'r' str[10] <-'a' str[11] <-'m' str[12] <-'m' str[13] <-'i' str[14] <-'n' str[15] <-'g' str[16] <- 'E' str[17] <- 'x' str[18] <- 'p' str[19] <- 'e' str[20] <- 'r' str[21] <- 't' str[22] <- '.' str[23] <- 'c' str[24] <- 'o' str[25] <- 'm' str[26] <- '\0' str[27] <- 'garbage value' str[28] <- 'garbage value' str[29] <- 'garbage value'when we use the function strlwr(str) it will convert all characters stored in the character array str to lowercase.Then stored like this
str[0] <- 'w' str[1] <- 'w' str[2] <- 'w' str[3] <- '.' str[4] <- 'c' str[5] <- 'p' str[6] <- 'r' str[7] <- 'o' str[8] <- 'g' str[9] <- 'r' str[10] <-'a' str[11] <-'m' str[12] <-'m' str[13] <-'i' str[14] <-'n' str[15] <-'g' str[16] <- 'e' str[17] <- 'x' str[18] <- 'p' str[19] <- 'e' str[20] <- 'r' str[21] <- 't' str[22] <- '.' str[23] <- 'c' str[24] <- 'o' str[25] <- 'm' str[26] <- '\0' str[27] <- 'garbage value' str[28] <- 'garbage value' str[29] <- 'garbage value'so after using the function strlwr(str) the value of string str is"www.cprogrammingexpert.com"
Example:
string function : strupr()
Syntax:char * strupr(char *str);
strupr(str) coverts str to all uppercase. It returns a pointer to the original input string str.
This function will convert all the lowercase characters in the string, which is provided as parameter, into uppercase. It will convert every lowercase to uppercase.
For example #include<stdio.h> #include<conio.h> int main() { char str[30]="www.CProgrammingExpert.com"; clrscr(); printf("str : %s\n",str); printf("strupr(str) : %s\n",strupr(str)); getch(); return 0; }here we declares the string str like this str[30]="www.CProgrammingExpert.com"; The string string str is a character array of size 30.The character is stored like this
str[0] <- 'w' str[1] <- 'w' str[2] <- 'w' str[3] <- '.' str[4] <- 'C' str[5] <- 'P' str[6] <- 'r' str[7] <- 'o' str[8] <- 'g' str[9] <- 'r' str[10] <-'a' str[11] <-'m' str[12] <-'m' str[13] <-'i' str[14] <-'n' str[15] <-'g' str[16] <- 'E' str[17] <- 'x' str[18] <- 'p' str[19] <- 'e' str[20] <- 'r' str[21] <- 't' str[22] <- '.' str[23] <- 'c' str[24] <- 'o' str[25] <- 'm' str[26] <- '\0' str[27] <- 'garbage value' str[28] <- 'garbage value' str[29] <- 'garbage value'whe we use the function strupr(str) it will convert all characters stored in the character array str to uppercase.Then stored like this
str[0] <- 'W' str[1] <- 'W' str[2] <- 'W' str[3] <- '.' str[4] <- 'C' str[5] <- 'P' str[6] <- 'R' str[7] <- 'O' str[8] <- 'G' str[9] <- 'R' str[10] <-'A' str[11] <-'M' str[12] <-'M' str[13] <-'I' str[14] <-'N' str[15] <-'G' str[16] <- 'E' str[17] <- 'X' str[18] <- 'P' str[19] <- 'E' str[20] <- 'R' str[21] <- 'T' str[22] <- '.' str[23] <- 'C' str[24] <- 'O' str[25] <- 'M' str[26] <- '\0' str[27] <- 'garbage value' str[28] <- 'garbage value' str[29] <- 'garbage value'so after using the function strupr(str) the value of string str is"WWW.CPROGRAMMINGEXPERT.COM" Example:
strcat()
Syntax:#include<string.h>
char *strcat( char *str1, const char *str2 );
strcat(str1,str2) concatenates a copy of the string pointed to by str2 to the the string pointed to by str1 and terminates str1 with a null. The first character of str2 overwrites the null terminator originally ending sr1. The str2 is untouched by the operation.In this function we will pass two arguments str1,str2 as parameters.
For example, Consider this program
#include<stdio.h> #include<conio.h> int main() { char str1[30]="www.cprogramming"; char str2[15]="expert.com"; clrscr(); printf("%d\t%d\nn",strlen(str1),strlen(str2)); printf("\n\nstrcat(str1,str2) : %s\n\n",strcat(str1,str2)); printf("\n\nstr1 : %s\t\tstr2 : %s\n\n",str1,str2); getch(); return 0; }Here
str1[30]= www.cprogramming and str2[15]=expert.com
if we are using function as strcat(str1,str2) and print the output. It will be www.cprogrammingexpert.com.
i.e the last character in the first parameter get erased and second parameter concatenated in memory str1 is stored like this "www.cprogramming\0" and str2 like this "expert.com\0" after concatenation str1 is "www.cprogrammingexpert.com\0" and str2 is"expert.com\0".
So make sure that the size of str1 is large enough to accomodate all the characters in str1 and str2.
str1[0] <- 'w' str1[1] <- 'w' str1[2] <- 'w' str1[3] <- '.' str1[4] <- 'c' str1[5] <- 'p' str1[6] <- 'r' str1[7] <- 'o' str1[8] <- 'g' str1[9] <- 'r' str1[10] <-'a' str1[11] <-'m' str1[12] <-'m' str1[13] <-'i' str1[14] <-'n' str1[15] <-'g' str1[16] <- '\0' str1[17] <- 'garbage value' str1[18] <- 'garbage value' str1[19] <- 'garbage value' str1[20] <- 'garbage value' str1[21] <- 'garbage value' str1[22] <- 'garbage value' str1[23] <- 'garbage value' str1[24] <- 'garbage value' str1[25] <- 'garbage value' str1[26] <- 'garbage value' str1[27] <- 'garbage value' str1[28] <- 'garbage value' str1[29] <- 'garbage value' str2 is str2[0] <- 'e' str2[1] <- 'x' str2[2] <- 'p' str2[3] <- 'e' str2[4] <- 'r' str2[5] <- 't' str2[6] <- '.' str2[7] <- 'c' str2[8] <- 'o' str2[9] <- 'm' str2[10] <- '\0' str2[11] <- 'garbage value' str2[12] <- 'garbage value' str2[13] <- 'garbage value' str2[14] <- 'garbage value' After concatenation str1[0] <- 'w' str1[1] <- 'w' str1[2] <- 'w' str1[3] <- '.' str1[4] <- 'c' str1[5] <- 'p' str1[6] <- 'r' str1[7] <- 'o' str1[8] <- 'g' str1[9] <- 'r' str1[10] <-'a' str1[11] <-'m' str1[12] <-'m' str1[13] <-'i' str1[14] <-'n' str1[15] <-'g' str1[16] <- 'e' str1[17] <- 'x' str1[18] <- 'p' str1[19] <- 'e' str1[20] <- 'r' str1[21] <- 't' str1[22] <- '.' str1[23] <- 'c' str1[24] <- 'o' str1[25] <- 'm' str1[26] <- '\0' str1[27] <- 'garbage value' str1[28] <- 'garbage value' str1[29] <- 'garbage value'In this example I have declared size of str1 equal to 30 and str2 equal to 15.
Now you may think why I have declared str1 as 30 and str2 as 15. It's just because I want you to undestand the concepts .OK
So if you know exact size of the string declare that size to save memory. Here str1[27] is enough, str2[11] is enough.
One more thing , what will happen if you are declaring the string like char *str1,*str2 , here no garbage values will come.
Before concatenation size of str1 is 16 & str2 is 10 and a fter concatenation size of str1 is 26 & str2 is 10. so we Saved memory. Example:
|
strncat()
Syntax:#include<string.h>
char *strncat( char *str1, const char *str2,size_t count );
strncat(str1,str2,n) concatenates not more than n characters of the string pointed to by str2 to the the string pointed to by str1 and terminates str1 with a null. The first character of str2 overwrites the null terminator originally ending sr1. The str2 is untouched by the operation.
Example:
strcpy()
Syntax:#include<string.h>
char *strcpy( char *to, const char *from );
strcpy(str1,str2) copies the string pointed to by str2 to the string pointed to by str1 and terminates str1 with a null. The str2 must be a pointer to a null terminated string.
Example:
strncpy()
Syntax:#include<string.h>
char *strncpy( char *to, const char *from,size_t count );
strncpy(str1,str2,n) copies up to n characters from the string pointed to by str2 to the string pointed to by str1 and terminates str1 with a null. The str2 must be a pointer to a null terminated string.
Example:
|
Hi, There is a small mistake in this program. Do you know ? Here |
strcmp()
Syntax:#include<string.h>
int strcmp( char *str1, char *str2 );
strcmp(str1,str2) compares str1 and str2 lexicographically .Returns a negative value if str1<str2; 0 if str1 and str2 are identical; and positive value if str1>str2.
Example -1:
|
|
strcmpi()
Syntax:#include<string.h>
int strcmpi( char *str1, char *str2 );
strcmpi(str1,str2) compares str1 and str2 lexicographically without regards to case .Returns a negative value if str1<str2; 0 if str1 and str2 are identical; and positive value if str1>str2.
Example:
strrev()
Syntax:#include<string.h>
char *str( char *str);
strrev(str) reverses all characters in str(except for the terminating null ). Returns a pointer to the reversed string.
Example:
No comments:
Post a Comment