- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C String Functions
![]() Share with a Friend |
C Programming - C String Functions
C String Functions
In C, strings are arrays of characters terminated by a null character (\0). The C Standard Library provides a collection of functions for manipulating strings, which are declared in the header file <string.h>.
Here is a list of some commonly used string functions in C:
- strlen()
- Purpose: Calculates the length of a string (excluding the null character \0).
- Syntax:
C
size_t strlen(const char *str);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of the string: %zu\n", strlen(str)); // Output: 13
return 0;
}
- strcpy()
- Purpose: Copies a string from the source to the destination.
- Syntax:
C
char *strcpy(char *dest, const char *src);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
strcpy(dest, src);
printf("Destination string: %s\n", dest); // Output: Hello
return 0;
}
- strncpy()
- Purpose: Copies a specified number of characters from the source to the destination.
- Syntax:
C
char *strncpy(char *dest, const char *src, size_t n);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[20];
strncpy(dest, src, 5); // Copies first 5 characters<
dest[5] = '\0'; // Null-terminate the string manually
printf("Destination string: %s\n", dest); // Output: Hello
return 0;
}
- strcat()
- Purpose: Concatenates (appends) one string to the end of another.
- Syntax:
C
char *strcat(char *dest, const char *src);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2); // Concatenate str2 to str1
printf("Concatenated string: %s\n", str1); // Output: Hello, World!
return 0;
}
- strncat()
- Purpose: Concatenates a specified number of characters from the source string to the destination string.
- Syntax:
C
char *strncat(char *dest, const char *src, size_t n);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, ";
char str2[] = "World!";
strncat(str1, str2, 3); // Concatenate first 3 characters of str2 to str1
printf("Concatenated string: %s\n", str1); // Output: Hello, Wor
return 0;
}
- strcmp()
- Purpose: Compares two strings lexicographically (dictionary order).
- Returns 0 if both strings are equal.
- Returns a negative value if the first string is lexicographically less than the second.
- Returns a positive value if the first string is lexicographically greater than the second.
- Syntax:
C
int strcmp(const char *str1, const char *str2);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result < 0)
printf("'%s' is less than '%s'.\n", str1, str2);
else
printf("'%s' is greater than '%s'.\n", str1, str2);
return 0;
}
- strncmp()
- Purpose: Compares the first n characters of two strings.
- Syntax:
C
int strncmp(const char *str1, const char *str2, size_t n);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "apricot";
int result = strncmp(str1, str2, 3); // Compare first 3 characters
if (result == 0)
printf("The first 3 characters are equal.\n");
else
printf("The first 3 characters are different.\n");
return 0;
}
- strchr()
- Purpose: Finds the first occurrence of a character in a string.
- Syntax:
C
char *strchr(const char *str, int c);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ch = strchr(str, 'o');
if (ch)
printf("First occurrence of 'o' is at position: %ld\n", ch - str);
else
printf("'o' not found.\n");
return 0;
}
- strrchr()
- Purpose: Finds the last occurrence of a character in a string.
- Syntax:
C
char *strrchr(const char *str, int c);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ch = strrchr(str, 'o');
if (ch)
printf("Last occurrence of 'o' is at position: %ld\n", ch - str);
else
printf("'o' not found.\n");
return 0;
}
- strstr()
- Purpose: Finds the first occurrence of a substring within a string.
- Syntax:
C
char *strstr(const char *haystack, const char *needle);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *substr = strstr(str, "World");
if (substr)
printf("Substring found at position: %ld\n", substr - str);
else
printf("Substring not found.\n");
return 0;
}
- strtok()
- Purpose: Tokenizes a string into smaller substrings based on a delimiter.
- Syntax:
C
char *strtok(char *str, const char *delim);
- Example:
C
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World, C, Programming";
char *token = strtok(str, ", ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, ", ");
}
return 0;
}
- strdup() (Non-Standard)
- Purpose: Duplicates a string by allocating memory and copying the contents of the string.
- Syntax:
C
char *strdup(const char *str);
- Example:
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[] = "Hello, World!";
char *copy = strdup(str);
printf("Original: %s\n", str);
printf("Duplicate: %s\n", copy);
free(copy); // Don't forget to free the allocated memory
return 0;
}
Summary:
C provides a range of string manipulation functions that allow you to perform tasks like string comparison, concatenation, searching, and more. Understanding and using these functions effectively can help you manage and manipulate strings in C programs. Make sure to include the <string.h> header file to access these functions.
