String is an array of characters. In this guide, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions.

The strlen function takes a string as an argument and returns its length. The returned value is of type long int. It is defined in the string.h header file. Jul 03, 2007  Syntax for Declaring Functions in Dev-C G'Day all, I'm trying to write programs that use multiple functions, but I can't seem to figure out the syntax I need to use. I'm using the following syntax, and keep getting errors regarding undeclared functions. Because compiler saves the string literals in the read-only memory (at least most.

We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations. We can perform such operations using the pre-defined functions of “string.h” header file. In order to use these string functions you must include string.h file in your C program.

String Declaration


Method 1:

These versions are obtaining added features which assist in the direction of the songs musicians wishing to make songs file by employing a range of songs resources. Auto-Tune is able to sustain the innovative strength of the initial audio while altering only period and timbre. You may have been utilized its two the majority of useful settings like automated mode and graphics setting. These songs modifying device has Antares AutoTune Premium Version characteristic to tune-up slow songs data files. Auto tune evo vst audacity 64 bit download.

Method 2: The above string can also be defined as

In the above declaration NULL character (0) will automatically be inserted at the end of the string.

What is NULL Char “0”?
'0' represents the end of the string. It is also referred as String terminator & Null Character.

String I/O in C programming

Read & write Strings in C using Printf() and Scanf() functions

Output:

Note: %s format specifier is used for strings input/output

Read & Write Strings in C using gets() and puts() functions

C – String functions

C String function – strlen

Syntax:

size_t represents unsigned short
It returns the length of the string without including end character (terminating char ‘0’).

Example of strlen:

Output:

strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array. So if I consider the above example again then the following statements would return the below values.

strlen(str1) returned value 13.
sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function).

String Functions In Dev C Pdf

C String function – strnlen

Syntax:

Classic looney tunes full episodes

size_t represents unsigned short
It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Example of strnlen:

Output:
Length of string str1 when maxlen is 30: 13
Length of string str1 when maxlen is 10: 10

Have you noticed the output of second printf statement, even though the string length was 13 it returned only 10 because the maxlen was 10.

C String function – strcmp

It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison.

If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value.
If string1 string2 then you would get 0(zero) when you use this function for compare strings.

Example of strcmp:

Output:

C String function – strncmp

size_t is for unassigned short
It compares both the string till n characters or in other words it compares first n characters of both the strings.

Example of strncmp:

Output:

C String function – strcat

It concatenates two strings and returns the concatenated string.

Example of strcat:

Output:

C String function – strncat

It concatenates n characters of str2 to string str1. A terminator char (‘0’) will always be appended at the end of the concatenated string.

Example of strncat:

Output:

C String function – strcpy

String Function In Dev C++

It copies the string str2 into string str1, including the end character (terminator char ‘0’).

Example of strcpy:

Output:

C String function – strncpy

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several terminator chars(‘0’) to accumulate the length of str1 to make it n.

C++

Example of strncpy:

Output:

C String function – strchr

It searches string str for character ch (you may be wondering that in above definition I have given data type of ch as int, don’t worry I didn’t make any mistake it should be int only. The thing is when we give any character while using strchr then it internally gets converted into integer for better searching.

Example of strchr:

Output:

C String function – Strrchr

It is similar to the function strchr, the only difference is that it searches the string in reverse order, now you would have understood why we have extra r in strrchr, yes you guessed it correct, it is for reverse only.

Now let’s take the same above example:

Output:

Why output is different than strchr? It is because it started searching from the end of the string and found the first ‘f’ in function instead of ‘of’.

C String function – strstr

It is similar to strchr, except that it searches for string srch_term instead of a single char.

Example of strstr:

Output:

C Programming String Functions

You can also use this function in place of strchr as you are allowed to give single char also in place of search_term string.

  • The C Standard Library
  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading

The string.h header defines one variable type, one macro, and various functions for manipulating arrays of characters.

Library Variables

Following is the variable type defined in the header string.h −

Sr.No.Variable & Description
1

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

Library Macros

String Functions In Dev C Example

Following is the macro defined in the header string.h −

Sr.No.Macro & Description
1

NULL

This macro is the value of a null pointer constant.

Library Functions

String Functions In Dev C Download

Following are the functions defined in the header string.h −

Sr.No.Function & Description
1void *memchr(const void *str, int c, size_t n)

Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

2int memcmp(const void *str1, const void *str2, size_t n)

Compares the first n bytes of str1 and str2.

3void *memcpy(void *dest, const void *src, size_t n)

Copies n characters from src to dest.

4void *memmove(void *dest, const void *src, size_t n)

Another function to copy n characters from str2 to str1.

5void *memset(void *str, int c, size_t n)

Copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str.

6char *strcat(char *dest, const char *src)

Appends the string pointed to, by src to the end of the string pointed to by dest.

7char *strncat(char *dest, const char *src, size_t n)

Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long.

8char *strchr(const char *str, int c)

Searches for the first occurrence of the character c (an unsigned char) in the string pointed to, by the argument str.

9int strcmp(const char *str1, const char *str2)

Compares the string pointed to, by str1 to the string pointed to by str2.

10int strncmp(const char *str1, const char *str2, size_t n)

Compares at most the first n bytes of str1 and str2.

11int strcoll(const char *str1, const char *str2)

Compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location.

12char *strcpy(char *dest, const char *src)

Copies the string pointed to, by src to dest.

13char *strncpy(char *dest, const char *src, size_t n)

Copies up to n characters from the string pointed to, by src to dest.

14size_t strcspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters not in str2.

15char *strerror(int errnum)

Searches an internal array for the error number errnum and returns a pointer to an error message string.

16size_t strlen(const char *str)

Computes the length of the string str up to but not including the terminating null character.

17char *strpbrk(const char *str1, const char *str2)

Finds the first character in the string str1 that matches any character specified in str2.

18char *strrchr(const char *str, int c)

Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str.

19size_t strspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters in str2.

20char *strstr(const char *haystack, const char *needle)

Finds the first occurrence of the entire string needle (not including the terminating null character) which appears in the string haystack.

21char *strtok(char *str, const char *delim)

Breaks string str into a series of tokens separated by delim.

22size_t strxfrm(char *dest, const char *src, size_t n)

Transforms the first n characters of the string src into current locale and places them in the string dest.

Coments are closed
Scroll to top