what everybody ought to know about string in c.
String in C
You will be able to :
String
In C programming, array of character are called strings. A string is terminated by null character /0.
Array is the collection of similar data under common name.
A common name is the name of string or array.For example:
char stringname = "Hello";Here stringname is common name .We will study String Decleration below Section.
C compiler represent above string as below image (string representation)
Declaration of String
It is declared as:
char string_name[size];
E.g: char name[10];
Strings can also be declared using pointer.
char *p
Initialization of String
It can be initialized when they are declared
char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','\0'}; OR; char c[5]={'a','b','c','d','\0'}; .
String can also be initialized using pointers
char *c="abcd";
String Handling Function
There are different libraries or built-in function in C for string manipulation.These function are defined in header file <string.h>
All string manipulation can be done manually by the programmer but, this makes programming complex and large.
To solve this, the C supports a large number of string handling functions.
strlen() - Calculates the length of string strcpy() - Copies a string to another string strcat() - Concatenates(joins) two strings strcmp() - Compares two string strlwr() - Converts string to lowercase strupr() - Converts string to uppercase
1. strlen():
This is used to return the length of string
It returns the number of characters in the string without including null
character
Syntax:
integer_variable = strlen(string);
2 strcpy() :
It copies one string to another
Syntax:
strcpy(destination_string , source_string);
e.g strcpy(a,b) i.e the content of b is copied to a
3. strcat():
It joins to strings together
It stores the content of second string at the end of the first one.
E.g: strcat (string1 , string2 ); i.e string1 = string1 + string2;
4. strcmp():
It compares two string
It takes two strings as parameters and returns the integer value.
>> 0 if both string are same
>> less than zero if the first string is less than the second one.
>>Greater than zero if the first string is more than the second
5. strrev () :
It is used to reverse all character of string except the null character
Syntax:
strrev ( string );
E.g strrev(str) i.e it reverses the characters in string str and stores the reversed string in the str
SUMMERY
- Strings are actually one-dimensional array of characters terminated by a null character '\0'.
- Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.
- Here "onlineclab" is a string of length 10 but in c it's length is 11 because c add '\0' at the end.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.
- The terminating null character '\0' is important because it is only way the string handling function can know where the string ends
- Strings can also be declared using pointer.
- Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. Array store all type of data but string store only character.
- string.h is collection of functions for string manipulation
- No standard operators for string assignment and comparisons!(remember: strings are arrays!)
- String can be passed to function in similar manner as arrays as, string is also an array
- Functions gets() and puts() are two string functions to take string input from user and display string respectively
- strlen() Calculates the length of string
- strcpy() Copies a string to another string
- strcat() Concatenates(joins) two strings
- strcmp() Compares two string
- strlwr() Converts string to lowercase
- strupr() Converts string to uppercase
- All string are array but all array are not string.
Download above tutorial:
You can Browse related article below for more information and program code related to string
Does above is helpful , Post you views in comment
Comments
Post a Comment