• Follow Us

Strings in C

In C Programming Language an array of characters is known as Strings. In terms of size of Strings, it always holds an extra character ‘\0’. Therefore the size of strings is equal to the total number of character+1.

Declaration of strings:

String declarations are as simple as declaring a one-dimensional array.

Here’s the basic syntax for string declaration.

char strname[size];

As mentioned above, there is an extra terminating character which is the Null character (‘\0’) used to indicate the termination of string which differs strings from normal character arrays.

Initializing a String:

The string can be initialized in different ways. We will explain this with an example. The following is an example to declare a string named str and initialize it with “CodeatGlance”.

1. char str[] = "CodeatGlance";
2. char str[50] = "CodeatGlance";
3. char str[] = {'C','o','d','e','a','t','G','l','a','n','c','e','\0'};
4. char str[14] = {'C','o','d','e','a','t','G','l','a','n','c','e','\0'};

Example:

#include <stdio.h>
int main () {
   char name[14] = {'C','o','d','e','a','t','G','l','a','n','c','e','\0'};
   printf("Name: %s\n", name );
   return 0;
}

Output:

Name: CodeatGlance

Few String functions in C

  1. strcat – concatenate two strings
  2. strchr – string scanning operation
  3. strcmp – compare two strings
  4. strcpy – copy a string
  5. strlen – get the string length
  6. strncat – concatenate one string with part of another
  7. strncmp – compare parts of two strings
  8. strncpy – copy part of a string
  9. memchr – Find a byte in memory
  10. memcmp – Compare bytes in memory
  11. memcpy – Copy bytes in memory
  12. memmove – Copy bytes in memory with overlapping areas
  13. memset – Set bytes in memory
  14. strcoll – Compare bytes according to a locale-specific collating sequence
  15. strcspn – Get the length of a complementary substring
  16. strerror – Get error message
  17. strpbrk – Scan a string for a byte
  18. strspn – Get the length of a substring
  19. strstr – Find a substring
  20. strtok – Split a string into tokens
  21. strxfrm – Transform string

C String function – strlen

Syntax:

size_t strlen(const char *str)

It returns the length of the string without including end character (terminating char ‘\0’).

Example of strlen:

#include <stdio.h>
#include <string.h>
int main()
{
     char str1[20] = "CodeatGlance";
     printf("Length of string str1: %d", strlen(str1));
     return 0;
}

Output:

Length of string str1: 12

C String function – strnlen

Syntax:

size_t strnlen(const char *str, size_t maxlen)

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

Example of strnlen:

#include <stdio.h>

#include <string.h>

int main()

{

char str1[20] = "CodeatGlance";

printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));

printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));

return 0;

}

Output:

Length of string str1 when maxlen is 30: 12

Length of string str1 when maxlen is 10: 10

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

C String function – strcmp

Syntax:

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

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 a positive value.

If string1 == string2 then you would get 0(zero) when you use this function for compare strings.

Example of strcmp:

#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "CodeatGlance";
char s2[20] = "CodeatGlance.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}

Output:

string 1 and 2 are different

C String function – strncmp

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

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:

#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "CodeatGlance";
char s2[20] = "CodeatGlance.COM";
/* below it is comparing first 8 characters of s1 and s2*/
if (strncmp(s1, s2, 8) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}

Output:

string1 and string 2 are equal

C String function – strcat

char *strcat(char *str1, char *str2)

It concatenates two strings and returns the concatenated string.

Example of strcat:

#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}

Output:

Output string after concatenation: HelloWorld

C String function – strncat

char *strncat(char *str1, char *str2, int n)

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:

char *strcpy( char *str1, char *str2)

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

Example of strcpy:

#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}

Output:

String s1 is: string 2: I’m gonna copied into s1

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.

Example of strncpy:

#include <stdio.h>
#include <string.h>
int main()
{
char first[30] = "string 1";
char second[30] = "string 2: I’m using strncpy now";
/* this function has copied first 10 chars of s2 into s1*/
strncpy(s1,s2, 12);
printf("String s1 is: %s", s1);
return 0;
}

Output:

String s1 is: string 2: I’m

C String function – strchr

char *strchr(char *str, int ch)

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:

#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strchr(mystr, 'f'));
return 0;
}

Output:

f function strchr

C String function – Strrchr

char *strrchr(char *str, int ch)

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:

#include <stdio.h>
#include <string.h>

int main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strrchr(mystr, 'f'));
return 0;
}

Output:

function strchr

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

char *strstr(char *str, char *srch_term)

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

Example of strstr:

#include <stdio.h>
#include <string.h>

int main()
{
char inputstr[70] = "String Function in C at CodeatGlance.COM";
printf ("Output string is: %s", strstr(inputstr, 'Begi'));
return 0;
}

Output:

Output string is: CodeatGlance.COM

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart