String functions C

  • strlen
  • strcmp
  • strcpy
  • strcat

C provides a number of in-built string handling functions which can be used directly on strings for getting length of strings, comparing, concatenating string and so on.
Following table lists some of these library functions and their purpose.

library functions and their purpose

Let us see how strlen ( ), strcmp ( ), strcpy ( ), strcat ( ) can be implemented using arrays as well as pointers. Also how they are used will be seen with an example of each. The name of the string functions suggests their purpose in most cases. The declaration of all these functions is in the header file “string.h”. Hence it is a must to include this header file when we use them.

Note: functions implemented using array are appended with an ‘a’ at the beginning of the function name. functions implemented using pointers are appended with a ‘p’ at the beginning of the function name.

E.g. for strlen()
Name of function using arrays is astrlen ( )
Name of function using pointers is pstrlen ( )

strlen()

  • Implementing strlen.
  • Using strlen.

As name suggests, this function is used to get the length of a string i.e. the number of character in a string, excluding the terminating character ‘�’.<

Int strlen (const char * s);

It accepts address of a string as an argument and returns an integer i.e. length of a string. Can you imagine how this function would have been written? It is simple. Let us implement it using array of characters as well as pointer to character.

Implementing strlen ( )

  • Array subscript version.
  • Pointer version.

Function strlen is already provided and its prototype is written in the header file string.h. We write function astrlen and pstrlen which imitate strlen. Function astrlen uses array to accommodate the string while function pstrlen uses a pointer equivalent to point to the string.

Array subscript version by using astrlen function

Pointer version by using pstrlen function

All that the function does is, it counts the number of character till string terminating character ‘�’ is encountered.

Using strlen() The following program shows use of function strlen to calculate length of character array arr.

strcmp()

  • Implementing strcmp.
  • Using strcmp

This function compares two strings.

int strcmp (const char * s, const char * t);

It accepts base address of two strings. It returns negative, zero or positive if ‘s’ is less than, equal to or

Implementing strcmp

  • Array subscript version.
  • Pointer version.

Function strcmp is already provided and its prototype is written in the header file string.h. We will write function astrcmp which imitate strcmp. Function astrcmp uses array to accommodate the string while function pstrcmp uses a pointer equivalent to point to the string.

Array subscript version

/ * strcmp : will return

Pointer version

Strings ‘s’ and ‘t’ are compared character by character and the position where ‘s’ and ‘t’ do not match, the characters are subtracted which gives numeric difference, between ASCII values of non – matching characters, to get the return value. If end of string ‘s’ occurs before a mismatch, 0 is returned.

Using strcmp ()
The following program shows use of function strcmp to compare two strings.

Output
Enter 2 Strings Susan
Susan
First string is equal to second string

strcpy ()

  • Implementing strcpy
  • Using strcpy.

This function copies the content of one string to another.

char * strcpy (char *t, const char *s);

Base address of source string and target string are to be supplied to this function as arguments. It returns the target string.

Implementing strcpy()

  • Array index version.
  • Pointer version.

Function strcpy is already provided and its prototype is written in the header file string.h. We write functions astrcpy which imitate strcpy. Function astrcpy uses array to accommodate the string while function pstrcpy uses ‘s’ pointer equivalent to point to the string

Array index version

Pointer version

Strcpy copies the characters in source string into the target string, one by one. Care is to be taken to see that target string is large enough to hold the entire source string.

Using strcpy()
The following program shows use if function strcpy to copy source string to target string

Output
Source string = Sayonara
Target string = Sayonara

strcat()

  • Implementing strcat.
  • Using strcat.

This function concatenates the source string at the end of the target string. Concatenating is appending or adding.
E.g. “Good” and “work’ on concatenation gives “Good work”.

char *strcat (char *t, const char *s);

It accepts base address of two strings to be concatenated as arguments and returns the target string.

Implementing strcat()

  • Array index version.
  • Pointer version.

Function strcat is already provided and its prototype is written in the header file string.h. We write function astrcat and pstrcat which imitate strcat. Function astrcat uses array to accommodate the string while function pstrcat uses a pointer equivalent to point to the string.

Array index version

Pointer version

The target string has to be large enough to hold the resulting string.

Using strcat() The following program shows use of function strcat to concatenate source string to target string.

Output
Source string =Hello
Target string = world!

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics