Home | » | C Programming | » | C Strings |
When you press any key from the keyboard, then it is said to be a character. But when you press more than one key, it becomes a string. So combination of characters (group of characters) is called string. "I am a good boy." is a string. We can print or display the string by using the printf() function as:
printf("\n I am a good boy");
Contents
- Operations on String
- String Handling Functions
- A program to concatenate two string
- A program to compare two string
- A program to copy one string into another string
- A program to print the length of String
- A program to print the reverse String
We can print a string by using control string %s or %[..] or %[^] i.e the declaration is as follows:
char name[10];
printf("%s",name); /*to print a single string*/
or
char name[5][10];
for(i=0;i<5;i++) /*to print a multiple string*/
{
printf("%s",name[i]);
}
Operations on String
String has number of operations in the C-Language, but some commonly used are as follow:
- Initialization of String variable
- Reading and Writing of String
- Combining strings together
or
Concatenation of two or more than two strings - Copy one string to another
- Comparing two strings
- Extracting a portion of string
or
To display a sub-string from the string - To reverse a string
- To find whether a string is palindrome or not
String Handling Functions
C-language is rich in library functions, but to handle or operate some operations with string, we use some powerful string handling functions. All these function are linked with the "string.h" header file strored in the include sub-directory in the Turbo-C compiler. Below the five commonly used string handling function as:
i) stcat()
The purpose of this string handling function strcat() is to concatenate or combine two different strings together.
The general syntax used for this is as:
Output is as :
Enter the two Names
Bintu
Chaudhary
BintuChaudhary
ii) stcmp()
The purpose of this function compare two strings. It will check which string is alphabetically above the others. For comparison ASCII (American Standard Code for Information Interchange) values are used
The general syntax used for this is as:
Output is as :
Enter the two Names
Bintu
Chaudhary
Both String are not equal
iii) strcpy()
The purpose of this function is to copy one string into another string. Note that target or destination field should be larger than the source filed. In other words size of the string1 should be larger to receive the contents of the string2.
The general syntax used for this is as:
Output is as :
Enter the two Names
Bintu
Chaudhary
Bintu Chaudhary
iv) strlen()
The purpose of this function is to count the number of character in a string i.e. to find the length of the string.
The general syntax used for this is as:
Output is as :
Enter the Name
Laura
Length of String=5
v) strrev()
The purpose of this function is to reverse a string.
The general syntax used for this is as:
Output is as :
Enter the Name
Laura
aruaL
3 comments
Click here for commentsnice article.thank you for you post.
Replyvisit
web programming tutorial
welookups
Nice article. Keep up the good work.
ReplyBuy the way check this link if anyone wants to learn c programming with infographics.
https://mycteacher.com/variables-in-c/
nice article for beginners.thank you.
Replyjava tutorial
ConversionConversion EmoticonEmoticon