C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • 1. strlen - String Length
  • 2. strcpy - String Copy
  • 3. strcat - String Concatenate
  • 4. strcmp - String Compare
  • 5. strncpy - String Copy with Length Limit

Was this helpful?

  1. Strings

Common String Functions

C programming provides a set of standard library functions for manipulating strings. These functions are declared in the <string.h> header file and offer various operations to work with character arrays. Let's explore some common string functions and their usage.

1. strlen - String Length

#include <string.h>

size_t strlen(const char *str);

This function returns the length of the input string, excluding the null character. It calculates the number of characters in the string.

const char *message = "Hello, World!";
size_t length = strlen(message); // Returns 13

2. strcpy - String Copy

#include <string.h>

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

This function copies the contents of the source string (src) to the destination string (dest). It includes the null character in the copied sequence.

char source[] = "Copy me!";
char destination[20];
strcpy(destination, source); // destination now contains "Copy me!"

3. strcat - String Concatenate

#include <string.h>

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

This function appends the contents of the source string (src) to the destination string (dest). The destination string must have enough space to accommodate both strings.

char greeting[20] = "Hello";
strcat(greeting, ", World!"); // greeting now contains "Hello, World!"

4. strcmp - String Compare

#include <string.h>

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

This function compares two strings (str1 and str2) lexicographically. It returns an integer less than, equal to, or greater than zero, indicating whether the first string is less than, equal to, or greater than the second string.

const char *string1 = "apple";
const char *string2 = "banana";
int result = strcmp(string1, string2); // Returns a value < 0

5. strncpy - String Copy with Length Limit

#include <string.h>

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

This function copies at most n characters from the source string (src) to the destination string (dest). It ensures that the destination is null-terminated if n is sufficient.

char source[] = "Copy me!";
char destination[5];
strncpy(destination, source, 4); // destination now contains "Copy"

These are just a few examples of the many string functions available in C. Understanding and using these functions appropriately will enhance your ability to work with strings in C programming.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousConstant Strings in CNextProgram: Bubble Sort

Was this helpful?