Jump to content
xisto Community
livepcportal

String.h Header File Functions

Recommended Posts

What is a string?

 

A string is a group of characters ended with null character. It is also known as array

of characters.

 

Declaration

 

char name_of_the_string;

 

where name of the string must be valid identifier & size must be an integer constant

 

Most useful functions available inside string.h header file

 

strcpy()

This function copies one string into another string.

Format: strcpy(s1,s2)

where s1 is a string variable and s2 is a string variable or string constant.

 

e.g. strcpy("Hello",s1);

will copy Hello to string variable s1.

 

strlen()

It counts the number of characters including null in the given string.

e.g. cout<<strlen("c++");

Output: 4

 

 

strcmp()

Format: strcmp(s1,s2);

 

This function will return 0 if two strings are equal, -1 if 1st string is smaller than 2nd and 1 if 1st string is greater than 2nd

This function is case sensitive & compares two strings by ASCII value of the characters.

 

 

strrev()

It prints given string into its reverse order

e.g. cout<<strrev("abc");

Output: cba

 

 

strcat()

This function add two strings.

Format: strcat(s1,s2)

e.g. cout<<strcat("abc","xyz");

Output:abcxyz

 

 

strupr()

It converts given string in uppercase.

Format: strupr(s1)

where s1 can be a string variable or a string constant.

 

 

 

strlwr()

It converts given string in lowercase. Format is same as that of strupr().

 

 

If you liked this post please rate it and if not then tell me why, your feedback will help me in boosting up

my confidence so that I can write more useful post in the future.

Edited by livepcportal (see edit history)

Share this post


Link to post
Share on other sites

...

 

2. strcmp()

 

Format: strcmp(s1,s2);

this function will return 0 if two strings are equal, -1 if 1st string is smaller than 2nd and 1 if 1st string is greater than 2nd

This function is case sensitive.

 

...

It's been a while since I've done C programming (funny how things that are no longer compulsory get pushed to one side, despite being fairly enjoyable), so I must admit that I've forgotten a little about how these things work, but I'm fairly sure you might need to explain this one a little more. If I remember correctly, the concept of "less than" and "greater" when it comes to strings is to do with the ASCII value of the characters involved, essentially treating the string as a 255-base number. This means capital versions of letters count as smaller than lower case, so will therefore be counted as alphabetically "before" in the context of this function.

 

More importantly, it means that any capital letter comes before any lower-case letter, due to the way ASCII is ordered. Hopefully this clarifies things a little, as it certainly got me pondering a bit at first! ;)

Share this post


Link to post
Share on other sites

It's been a while since I've done C programming (funny how things that are no longer compulsory get pushed to one side, despite being fairly enjoyable), so I must admit that I've forgotten a little about how these things work, but I'm fairly sure you might need to explain this one a little more. If I remember correctly, the concept of "less than" and "greater" when it comes to strings is to do with the ASCII value of the characters involved, essentially treating the string as a 255-base number. This means capital versions of letters count as smaller than lower case, so will therefore be counted as alphabetically "before" in the context of this function.

 

More importantly, it means that any capital letter comes before any lower-case letter, due to the way ASCII is ordered. Hopefully this clarifies things a little, as it certainly got me pondering a bit at first! ;)


Yes, you are right. 

 

strcmp() function compares the two string by the ASCII code of the characters.

 

 

A similar feature is given by strcmpi() function but it ignore cases of the characters.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.