Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

String Functions in C

Last Updated on April 24, 2023 by Prepbytes

String functions in C were introduced to simplify and standardize the process of working with strings in C programs. String functions in c are an essential part of many applications, having pre-defined functions to perform common operations.

String Functions in C

String functions in C are built-in functions that provide a convenient way to manipulate and process character arrays (strings). These functions can perform operations such as copying, concatenation, comparison, searching, and finding the length of a string.

Some String Functions in C

Here are some commonly used string functions in C:

  1. strlen(): This function can be used to find a string’s length.

    Syntax of srtlen():

    size_t strlen(const char *str);

    Example of strlen():

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello World";
        printf("The length of the string is: %zu\n", strlen(str));
        return 0;
    }
    
     

    Output:

    The length of the string is: 11
  2. strcpy(): This function is used to copy one string to another.

    Syntax of srtcpy():

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

    Example of srtcpy():

    #include <stdio.h>
    #include <string.h>
    
    int main() {
    char src[] = "Hello World";
    char dest[20];
    strcpy(dest, src);
    printf("The copied string is: %s\n", dest);
    return 0;
    }
    
     

    Output:

    The copied string is: Hello World
  3. strcat(): This function is used to concatenate two strings.

    Syntax of strcat():

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

    Example Code:

    #include 
    #include 
    
    int main() {
        char str1[] = "Hello";
        char str2[] = "World";
        strcat(str1, str2);
        printf("The concatenated string is: %s\n", str1);
        return 0;
    }
    

    Output:

    The concatenated string is: HelloWorld
  4. strcmp(): This function is used to compare two strings.

    Syntax of strcmp():

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

    Example Code:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[] = "Hello";
        char str2[] = "World";
        int result = strcmp(str1, str2);
        if(result == 0) {
            printf("The strings are equal.\n");
        }
        else if(result < 0) {
            printf("The first string is less than the second string.\n");
        }
        else {
            printf("The first string is greater than the second string.\n");
        }
        return 0;
    }
    

    Output:

    The first string is smaller than the second string.
  5. strupr(): It takes a string as input and converts all the letters in the string to uppercase.

    Syntax of strupr():

    char *strupr(char *str);

    Example Code:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello, World!";
        printf("Original string: %s\n", str);
        // Convert string to uppercase
        strupr(str);
        printf("Uppercase string: %s\n", str);
        return 0;
    }
    
    
     

    Output:

    Uppercase string: HELLO, WORLD!
  6. strlwr(): It takes a string as input and converts all the letters in the string to lowercase.

    Syntax of strlwr():

    char *strlwr(char *str);

    Example Code:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello, World!";
        printf("Original string: %s\n", str);    
        // Convert string to lowercase
        strlwr(str);
        printf("Lowercase string: %s\n", str);
        
        return 0;
    }
     

    Output:

    Lowercase string: hello, world!

  1. Strrev(): It is used to reverse a given string. It takes a string as an argument and returns a pointer to the reversed string.

    Syntax of strrev():

    char *strrev(char *str);

    Example Code:

    #include <stdio.h>
    #include <string.h>
    int main()
    {
        char str[40]; // declare the size of character string
        printf(" \n Enter a string to be reversed: ");
        scanf("%s", str);
    
        // use strrev() function to reverse a string
        printf(" \n After the reverse of a string: %s ", strrev(str));
        return 0;
    }
    
    
    
    
     

    Output:

  1. strstr(): It returns a pointer to the first occurrence of the given substring str2 within the given string str1

    Syntax of strstr():

    char *strstr(const char *str1, const char *str2);

    Example Code:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
       char str1[] = "hello world";
       char str2[] = "world";
       char *ptr;
       
       ptr = strstr(str1, str2);
       
       printf("Substring found at: %s\n", ptr);
    
       return 0;
    }
     

    Output:

    world

String Declaration in C

In C programming language, strings are declared as arrays of characters. Each character in the string is stored in a separate element of the array, and the last element of the array is a null character ‘\0’ which indicates the end of the string. Here’s how you can declare a string in C:

char str[SIZE];

In the above declaration, SIZE represents the size of the array that will hold the string, and char specifies the data type of the array elements, which in this case is a character.

Alternatively, you can also declare and initialize a string at the same time:

char str[] = "Hello World";

In this declaration, the size of the array is automatically calculated based on the length of the string "Hello World".

Advantages of String Function in C

Here are the advantages of using string functions in C:

  • String functions in C are efficient.
  • String functions in C are reusable.
  • String functions in C have a standardized interface and behaviour.
  • String functions in C are robust and handle edge cases and error conditions.
  • String functions in C prevent buffer overflows and other security problems.

Disadvantages of String Function in C

Here are some disadvantages of using string functions in C:

  • String functions in C can be limited in functionality.
  • String functions in C may not be optimal for specific use cases.
  • String functions in C may introduce security problems if used incorrectly.
  • String functions in C can be memory-intensive for large strings.
  • String functions in C may not be portable across different systems or architectures.

Conclusion
In conclusion, String functions in C provide a convenient and efficient way to manipulate strings. These functions, such as strlen(), strcpy(), strcat(), and strcmp(), allow for common string operations like finding their length, copying them, concatenating them, and comparing them. Careful use of these functions can lead to more robust and efficient programs.

Frequently Asked Questions(FAQs)

Q1 . What are some common string functions in C?
Ans: Some of the common string functions in C includes strlen(), strcpy(), strcat(), and strcmp(). These functions are used to manipulate strings, including finding their length, copying them, concatenating them, and comparing them.

Q2. How do you declare string functions in C?
Ans: string functions in c are declared as arrays of characters. Each character in the string is stored in a separate element of the array, and the last element of the array is a null character ‘\0’. For example, char str[SIZE];.

Q3. Can you modify string functions in C?
Ans: Yes, you can modify string functions in C. However, it’s important to keep in mind that C strings are null-terminated, and you must ensure that you add a null character ‘\0’ at the end of the modified string.

Q4. Can you use string functions with arrays of other data types?
Ans: No, string functions in C are specifically designed to work with arrays of characters. If you want to manipulate arrays of other data types, you’ll need to write your own functions.

5. What happens if you don’t add a null character ‘\0’ at the end of a string?
Ans: If you don’t add a null character at the end of a string, C will not know where the string ends, and you may encounter unexpected behaviour when you try to manipulate the string using string functions.This could lead to memory overflow along with additional security problems.

Leave a Reply

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