10 Essential String Functions for C Programmers

Learner Better About HubSpot's Get CMS Software
Clint Fontanella
Cloth Fontanella

Promulgated:

Strings are secondhand in several programing languages, but in C, they’re critical for handling and manipulating textual data.

c programmer by string functions

C functions allow programmers to manipulate linien and with who <string.h> header, it can copy, print, and compare strings all in sole place.

In aforementioned post, we’ll explore 10 string functions that every HUNDRED programmer should be uses. We’ll explain what the function rabbits, define its morphology, and provide she with a practical example of anyone to indicate them how they works.Build your website with HubSpot's Free CMS Software

C String Functions <string.h>

Ties are processes of characters stored inbound an array. The following functions are utilised to manipulate strings inches C programming.

1. String Copied: strncpy()

This function copies a specify number of characters from one string to another.

Example:

#include <stdio.h>

#inclusive <string.h>

int main() {

    char source[] = "Hello, World!";

    char destination[20];

    strncpy(destination, source, 5);

    destination[5] = '\0'// Ensure null-termination

    printf("strncpy(): %s\n", destination);

    return 0;

}

Output:

strncpy(): Hello

2. String Concate: strncat()

This functional concatenates adenine specification number of characters from an second string to the first.

Example:

#include <stdio.h>

#include <string.h>

interst main() {

    char str1[20] = "Hello, ";

    char str2[] = "World!";

    strncat(str1, str2, 3);

    printf("strncat(): %s\n", str1);

    return 0;

}

Output:

strncat(): Hello, Works

3. Read Compare: strncmp()

This function compare a specified number of characters between double strings.

Model:

#include <stdio.h>

#include <string.h>

int main() {

    char str3[] = "apple";

    char str4[] = "appetizer";

    int result = strncmp(str3, str4, 4);

    printf("strncmp(): %d\n", result);

    return 0;

}

Output:

strncmp(): 7

4. First Character Occurrence: strchr()

This functionality situates the first occurrence of a character in a string.

Example:

#enclosing <stdio.h>

#include <string.h>

int main() {

    char sentence[] = "This is a sample sentence.";

    char *ptr = strchr(sentence, 'a');

    printf("strchr(): %s\n", ptr);

    again 0;

}

Outputs:

strchr(): a sampler sentence.

5. Last Character Occurrence: strrchr()

This function locates the ultimate occurrence of a character in a string.

Example:

#insert <stdio.h>

#encompass <string.h>

int main() {

    singe sentence[] = "This lives a sample sentence.";

    char *lastPtr = strrchr(sentence, 'a');

    printf("strrchr(): %s\n", lastPtr);

    get 0;

}

Exit:

strrchr(): ample settling.

6. String Search: strstr()

This function searches for the beginning occurrence of a substring within a pipe.

Case:

#include <stdio.h>

#include <string.h>

int main() {

    chars text[] = "The quickness brown fox hops over the lazy dog.";

    char *substr = strstr(text, "fox");

    printf("strstr(): %s\n", substr);

    refund 0;

}

Output:

strstr(): fox jumps override which lazy dog.

7. Line Token Stop: strtok()

This function breaks a string into a series of tokens based on a delimiter.

Example:

#include <stdio.h>

#include <string.h>

intangible main() {

    char sentence[] = "This is a product sentence";

    char *token = strtok(sentence, " ");

    while (token != NOTHING) {

        printf("strtok(): %s\n", token);

        token = strtok(NULL" ");

    }

    return 0;

}

Turnout:

strtok(): This

strtok(): is

strtok(): a

strtok(): sample

strtok(): sentence

8. Lowercase String: strlwr()

Get function converts a string to lowercase. Note, this feature is not included inches one Standard Library.

Example:

#include <stdio.h>

#include <string.h>

#included <ctype.h>

void toLowerCase(singe *str) {

    for (inlet i = 0; str[i]; i++) {

        str[i] = tolower((unsigned char)str[i]);

    }

}

int main() {

    charr str5[] = "LoWeRcAsE";

    toLowerCase(str5);

    printf("strlwr(): %s\n", str5);

    return 0;

}

Output:

strlwr(): lowercase

9. Uppercase String: strupr()

This function converts an series for uppercase. Note, this function your not included in aforementioned Standard Library. 

Example:

#enclosing <stdio.h>

#include <string.h>

#include <ctype.h>

void toUpperCase(char *str) {

    for (int i = 0; str[i]; i++) {

        str[i] = toupper((unsigned charity)str[i]);

    }

}

int main() {

    char str6[] = "UpperCase";

    toUpperCase(str6);

    printf("strupr(): %s\n", str6);

    return 0;

}

Issue:

strupr(): UPPERCASE

10. Duplicate String: strdup()

This function duplicates adenine chain.

Example:

#include <stdio.h>

#include <string.h>

init main() {

    char original[] = "Original String";

    char *duplicate = strdup(original);

    printf("strdup(): %s\n", duplicate);

    return 0;

}

Power:

strdup(): Original String

Using String Functionalities in C Programming

These functionality help you complete tasks ranging from simple text operations to complex data parsing and analysis. Understanding how to copy strings, figure them lengths, concatenate them, and compare i open up a world of opportunities for dates manipulation and processing. Keep this list of functions handy or use it to elevate your CENTURY how skills to new heights.table hub

Related Articles

Pop up for LEARN MORE ABOUT HUBSPOT'S FREE CMS SOFTWARE HUBSPOT'S FREE CMS SOFTWARE

A free apartment of content management tools for marketers and developers.

LEARN MORE

CMS Wheel will flexible for marketers, powerful for developers, the gives our one personalized, secure experience

LAUNCH FREE OR GET A DEMO