STRINGS
In C programming, array of characters is called a string. A string is terminated by a null character \0
.
For example:
" c string tutorial "
Here, " c string tutorial " is a string. When, compiler encounter strings, it appends a null character \0
at the end of string.
Declaration of strings
Strings are declared in a similar manner as arrays . Only difference is that, strings are of char type
.
Using arrays
char s [ 5 ] ;
Using pointers
Strings can also be declared using pointer .
char * p ;
Initialization of strings
In C, string can be initialized in a number of different ways.
For convenience and ease, both initialization and declaration are done in the same step.
Using arrays
char c [ ] = " a b c d " ;
or ,
char c [ 50 ] = " a b c d " ;
or ,
char c [ ] = { ' a ' , ' b ' , ' c ' , ' d ' , ' \ 0 ' } ;
or ,
char c [ 5 ] = { ' a ' , ' b ' , ' c ' , ' d ' , ' \ 0 ' } ;
The given string is initialized and stored in the form of arrays as above.
Using pointers
String can also be initialized using pointers as:
char * c = " a b c d " ;
Reading Strings from user
You can use the scanf ( ) function to read a string like any other data types.
However, the scanf ( ) function only takes the first entered word. The function terminates when it encounters a white space (or just space).
Using scanf ( ) to read a string
1.Write a C program to illustrate how to read string from terminal.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/arrays/
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/arrays/
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/arrays/
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
char name [ 20 ] ;
clrscr ( ) ;
Printf ( " Enter name : \ n " ) ;
Scanf ( " % s " , & name ) ;
Printf ( " Your name is %s .", name ) ;
return 0 ;
}
Here, scanf ( ) function takes only a single string before the white space.
Reading a line of text
An approach to reading a full line of text is to read and store each character one by one.
Using getchar() to read a line of text
2. C program to read line of text to read line of text using gets ( ) and puts ( ) .
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
char name [ 20 ] ;
clrscr ( ) ;
Printf ( " Enter name : \ n " ) ;
gets ( name ) ; / / Function to read string from user.
Printf ( " Name : " ) ;
Puts ( name ) ; / / Function to display string.
return 0 ;
}
Passing Strings to Functions
Strings are just char
arrays. So, they can be passed to a function in a similar manner as arrays.
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
void displayString ( char str [ ] ) ;
int main ( )
{
char str [ 50 ] ;
clrscr ( ) ;
Printf ( " Enter string : \ n " ) ;
gets ( str ) ;
displayString ( str ) ; // Passing string to function.
return 0 ;
}
void displayString ( char str [ ] )
{
printf ( " String Output : " ) ;
Puts ( str ) ;
}
String Functions
You need to often manipulate strings according to the need of a problem. Most, if not all, of the time string manipulation can be done manually but, this makes programming complex and large.
To solve this, C supports a large number of string handling functions in the standard library "string.h"
.
Few commonly used string handling functions are discussed below:
Function | Work of Function |
---|---|
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 |
Strings handling functions are defined under "string.h"
header file.
C strlen ( )
The strlen() function calculates the length of a given string.
C strlen ( ) Prototype
size_t strlen ( const char *str ) ;
The function takes a single argument, i.e, the string variable whose length is to be found, and returns the length of the string passed.
The strlen()
function is defined in string.h header file.
Example: C strlen() function
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str [ 20 ] ;
clrscr ( ) ;
Printf ( " Enter string : \ n " ) ;
gets ( str ) ;
Printf ( " length of string is %d \ n " , strlen ( str ) ) ;
return 0 ;
}
C strcpy()
strcpy() Function prototype
char* strcpy (char* destination , const char* source ) ;
The strcpy() function copies the string pointed by source (including the null character) to the character array destination.
This function returns character array destination.
Example: C strcpy()
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str1 [ 10 ] = " awesome " ;
char str2 [ 10 ] ;
char str3 [ 10 ] ;
clrscr ( ) ;
strcpy ( str 2 , str1 ) ;
strcpy ( str 3 , " well " ) ;
puts ( str2 ) ;
puts ( str3 ) ;
return 0;
}
C strcat ( )
The function strcat() concatenates two strings.
In C programming, strcat() concatenates (joins) two strings.
C strcat() Prototype
char *strcat ( char * dest , const char *src )
It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument.
The pointer to the resultant string is passed as a return value.
Example: C strcat() function
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str1 [ ] = " taruni " , str2 [ ] = " computers " ;
clrscr ( ) ;
//concatenates str1 and str2 and resultant string is stored in str1.
strcat ( str1 ,str2 ) ;
puts ( str1 ) ;
puts ( str2 ) ;
return 0 ;
}
C strcmp()
C strcmp() Prototype
int strcmp (const char* str1, const char* str2);
The strcmp() function takes two strings and return an integer.
The strcmp() compares two strings character by character. If the first character of two strings are equal, next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.
Return Value from strcmp()
Return Value | Remarks |
---|---|
0 | if both strings are identical (equal) |
negative | if the ASCII value of first unmatched character is less than second. |
positive integer | if the ASCII value of first unmatched character is greater than second. |
Example: C strcmp ( ) function
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str1 [ ] = " abcd " , str2 [ ] = " abCd " , str3 [ ] = " abcd " ;
int result ;
Clrscr ( ) ;
// comparing strings str1 and str2
result = strcmp ( str1 , str2 ) ;
Printf ( " strcmp ( str1 , str2 ) = % d \ n ", result ) ;
// comparing strings str1 and str3
result = strcmp ( str1, str3 ) ;
Printf ( " strcmp ( str1, str3 ) = % d \ n " , result ) ;
return 0;
}
C – strlwr ( ) function
strlwr( ) function converts a given string into lowercase.
C strlwr ( ) Prototype
char *strlwr ( char *string ) ;
Example: C strlwr ( ) function
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str [ ] = " MUSE COMPUTERS " ;
clrscr ( ) ;
Printf ( " % s \ n ", strlwr ( str ) ) ;
return 0 ;
}
C – strupr ( ) function
strupr( ) function converts a given string into uppercase.
C strupr ( ) Prototype
char *strupr ( char * string ) ;
Example: C strupr ( ) function
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
# include < string.h >
int main ( )
{
char str [ ] = " muse computers " ;
clrscr ( ) ;
Printf ( " % s \ n ", strupr ( str ) ) ;
return 0 ;
}
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/functions/