C Programming + Tutorials in C for Beginners

C Strings

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


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:

  1. Initialization of String variable
  2. Reading and Writing of String
  3. Combining strings together
        or
    Concatenation of two or more than two strings
  4. Copy one string to another
  5. Comparing two strings
  6. Extracting a portion of string
        or
    To display a sub-string from the string
  7. To reverse a string
  8. 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:

  1. strcat()
  2. strcmp()
  3. strcpy()
  4. strlen()
  5. strrev()

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:

strcat(string1,string2);





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:

strcmp(string1,string2);





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:

strcpy(string1,string2);





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:

n=strlen(string);





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:

strrev(string);





Output is as :
Enter the Name
Laura
aruaL



<< Previous Topic
Next Topic >>



Previous
Next Post »

3 comments

Click here for comments
suman
admin
25 August 2018 at 20:27 ×

nice article.thank you for you post.
visit
web programming tutorial
welookups

Reply
avatar
Rahul
admin
30 December 2018 at 10:35 ×

Nice article. Keep up the good work.
Buy the way check this link if anyone wants to learn c programming with infographics.
https://mycteacher.com/variables-in-c/

Reply
avatar
suman
admin
4 August 2019 at 05:45 ×

nice article for beginners.thank you.
java tutorial

Reply
avatar
Thanks for your comment