bscodinglab

String

In C programming, a string is a sequence of characters that is terminated by a null character (‘\0’). A string can be represented as a one-dimensional character array.

Here’s a table of commonly used string functions in C, along with their brief descriptions and examples:

 

FunctionDiscriptionExample
Strlen()returns the length of a stringchar str[] = "Hello, world!";
int len = strlen(str);
printf("Length is %d", len);
Strcpy()copies one string to anotherchar str1[] = "Hello";
char str2[10];
strcpy(str2, str1);
printf("%s", str2);
Strcat()concatenates two stringschar str1[] = "Hello";
char str2[] = ", world!";
strcat(str1, str2);
printf("%s", str1);
Strcmp()compares two stringschar str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {...}
Strchr()searches for a character in a stringchar str[] = "Hello, world!";
char *ptr = strchr(str, 'w');
printf("%s", ptr);
Strstr()searches for a substring in a stringchar str[] = "Hello, world!";
char *ptr = strstr(str, "world");
printf("%s", ptr);
Strncpy()copies a specified number of characters
from one string to another
char str1[] = "Hello";
char str2[10];
strncpy(str2, str1, 3);
printf("%s", str2);
Strncat()concatenates a specified number of
characters from one string to another
char str1[] = "Hello";
char str2[] = ", world!";
strncat(str1, str2, 5);
printf("%s", str1);
Strncmp()compares a specified number of
characters from two strings
char str1[] = "Hello";
char str2[] = "Hell";
int result = strncmp(str1, str2, 4);
if (result == 0) {...}
Strtok()breaks a string into tokens using
a specified delimiter
char str[] = "Hello, world!";
char *ptr = strtok(str, ", ");
while (ptr != NULL) {...}
Sprint()writes formatted data to a stringchar str[20];
int num = 42;
sprintf(str, "%d", num);
printf("%s", str);
Snprintf()writes formatted data to
a string with a specified maximum length
char str[20];
int num = 42;
snprintf(str, 20, "%d", num);
printf("%s", str);
Strrev()reverses a stringchar str[20];
printf("Enter string: ");
gets(str);//reads string from console.
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
Some Example of Strings
  1. Strlen :- Calculates the length of a string, excluding the null character.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strcpy :- Copies a string to another string. The destination string must have enough space to hold the source string, including the null character.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strcat :- Concatenates two strings. The destination string must have enough space to hold both strings, including the null character.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strcmp :- Compares two strings lexicographically. Returns 0 if the strings are equal, a negative value if the first string is less than the second, or a positive value if the first string is greater than the second.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strchr :- Finds the first occurrence of a character in a string. Returns a pointer to the character, or NULL if the character is not found.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strstr :- Finds the first occurrence of a substring in a string. Returns a pointer to the start of the substring, or NULL if the substring is not found.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Sprint :- Formats a string and stores it in a buffer. The formatted string can include placeholders for variables.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

  1. Strtok :- Splits a string into tokens based on a delimiter. Returns a pointer to the next token, or NULL if there are no more tokens.

#include <stdio.h>
#include <string.h>
int main()
{
    // defining string
        char str[] = “Use String Length”;
    // getting length of str using strlen()
         int length = strlen(str);
         printf(“String Length is : %d”, length);
         return 0;
   }

        Output :-  String Length is : 17

These functions are all part of the standard C library, and can be included by adding #include <string.h> to your source code.