Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
* * * * - 1 votes

String.h Header File Functions


2 replies to this topic

#1 livepcportal

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 221 posts
  • Gender:Male
  • Location:India
  • myCENT:40.55

Posted 20 August 2009 - 03:24 PM

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[size];

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, 22 August 2009 - 03:52 PM.


#2 Mordent

    Premium Member

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 156 posts

Posted 20 August 2009 - 03:38 PM

View Postlivepcportal, on Aug 20 2009, 04:24 PM, said:

...

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! ;)

#3 livepcportal

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 221 posts
  • Gender:Male
  • Location:India
  • myCENT:40.55

Posted 21 August 2009 - 03:05 PM

View PostMordent, on Aug 20 2009, 03:38 PM, said:

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.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users