In this section, we will explore various functions provided by the C standard library to manipulate arrays and strings. Understanding these functions is crucial for efficient and effective programming in C.

Key Concepts

  1. Array Functions: Functions that help in manipulating arrays, such as sorting, searching, and copying.
  2. String Functions: Functions that handle string operations like concatenation, comparison, and length calculation.

Array Functions

  1. memcpy

The memcpy function copies a specified number of bytes from one memory location to another.

Syntax:

void *memcpy(void *dest, const void *src, size_t n);

Parameters:

  • dest: Pointer to the destination array where the content is to be copied.
  • src: Pointer to the source of data to be copied.
  • n: Number of bytes to copy.

Example:

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

int main() {
    char src[50] = "Hello, World!";
    char dest[50];

    memcpy(dest, src, strlen(src) + 1);
    printf("Destination: %s\n", dest);

    return 0;
}

  1. memset

The memset function fills a block of memory with a particular value.

Syntax:

void *memset(void *str, int c, size_t n);

Parameters:

  • str: Pointer to the block of memory to fill.
  • c: Value to be set.
  • n: Number of bytes to be set to the value.

Example:

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

int main() {
    char str[50] = "Hello, World!";
    
    memset(str, '*', 5);
    printf("Modified String: %s\n", str);

    return 0;
}

  1. memcmp

The memcmp function compares two blocks of memory.

Syntax:

int memcmp(const void *str1, const void *str2, size_t n);

Parameters:

  • str1: Pointer to the first block of memory.
  • str2: Pointer to the second block of memory.
  • n: Number of bytes to compare.

Example:

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

int main() {
    char str1[50] = "Hello, World!";
    char str2[50] = "Hello, World!";
    char str3[50] = "Hello, C!";

    int result = memcmp(str1, str2, strlen(str1));
    printf("Comparison Result (str1 vs str2): %d\n", result);

    result = memcmp(str1, str3, strlen(str1));
    printf("Comparison Result (str1 vs str3): %d\n", result);

    return 0;
}

String Functions

  1. strlen

The strlen function calculates the length of a string.

Syntax:

size_t strlen(const char *str);

Parameters:

  • str: Pointer to the null-terminated byte string.

Example:

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

int main() {
    char str[50] = "Hello, World!";
    size_t length = strlen(str);

    printf("Length of the string: %zu\n", length);

    return 0;
}

  1. strcpy

The strcpy function copies a string to another.

Syntax:

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

Parameters:

  • dest: Pointer to the destination array where the content is to be copied.
  • src: Pointer to the source string.

Example:

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

int main() {
    char src[50] = "Hello, World!";
    char dest[50];

    strcpy(dest, src);
    printf("Destination: %s\n", dest);

    return 0;
}

  1. strcat

The strcat function concatenates two strings.

Syntax:

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

Parameters:

  • dest: Pointer to the destination array, which should contain a C string and be large enough to contain the concatenated resulting string.
  • src: Pointer to the source string.

Example:

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

int main() {
    char dest[50] = "Hello, ";
    char src[50] = "World!";

    strcat(dest, src);
    printf("Concatenated String: %s\n", dest);

    return 0;
}

  1. strcmp

The strcmp function compares two strings.

Syntax:

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

Parameters:

  • str1: Pointer to the first string.
  • str2: Pointer to the second string.

Example:

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

int main() {
    char str1[50] = "Hello, World!";
    char str2[50] = "Hello, World!";
    char str3[50] = "Hello, C!";

    int result = strcmp(str1, str2);
    printf("Comparison Result (str1 vs str2): %d\n", result);

    result = strcmp(str1, str3);
    printf("Comparison Result (str1 vs str3): %d\n", result);

    return 0;
}

Practical Exercises

Exercise 1: Copying Arrays

Write a program to copy the contents of one integer array to another using memcpy.

Solution:

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

int main() {
    int src[] = {1, 2, 3, 4, 5};
    int dest[5];

    memcpy(dest, src, sizeof(src));
    
    printf("Destination Array: ");
    for(int i = 0; i < 5; i++) {
        printf("%d ", dest[i]);
    }
    printf("\n");

    return 0;
}

Exercise 2: String Length Calculation

Write a program to calculate the length of a string using strlen.

Solution:

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

int main() {
    char str[50] = "Calculate my length!";
    size_t length = strlen(str);

    printf("Length of the string: %zu\n", length);

    return 0;
}

Exercise 3: String Concatenation

Write a program to concatenate two strings using strcat.

Solution:

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

int main() {
    char dest[50] = "Hello, ";
    char src[50] = "C Programming!";

    strcat(dest, src);
    printf("Concatenated String: %s\n", dest);

    return 0;
}

Summary

In this section, we covered various array and string functions provided by the C standard library. These functions are essential for manipulating arrays and strings efficiently. We explored functions like memcpy, memset, strlen, strcpy, strcat, and strcmp, along with practical examples and exercises to reinforce the concepts. Understanding and utilizing these functions will significantly enhance your ability to handle data in C programs.

© Copyright 2024. All rights reserved