Strings in c language
Strings in C Programming Language
In C programming, strings represent sequences of characters stored in contiguous memory locations.
Strings
are essentially arrays of characters, terminated by a null character (\0), signifying the end of the string.
Declaration and Initialization: Strings can be declared and
initialized using array notation. For instance:
char str[] = "Hello, World!";
Here, str denotes an array of characters containing the string "Hello, World!", with the null character
automatically appended at the end.
Manipulation: String manipulation involves various functions from the
Character Access: Accessing individual characters within a string
is possible using array notation.
For
example:
char ch = str[0]; // Accesses the first character 'H'
Termination: The null character ('\0') signifies the end of a string.
Its presence is crucial as it marks
the string's endpoint. Omitting it may lead to undefined behavior, especially in functions processing
strings.
Usage: Strings find extensive use in C for tasks like input/output
operations, text processing, parsing, and
manipulation of textual data.
A solid grasp of strings is vital for C programmers, given their widespread application in various C-based
applications and systems.
standard library function: strlen(), strcpy(), strcat(), strcmp();
strlen() - String Length:
strlen() calculates the length of a given string, excluding the null terminator.
Syntax: size_t strlen(const char *str);
Example:
#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; int length = strlen(str); printf("Length of the string: %zu\n", length); return 0; }
strcpy() - String Copy:
strcpy() copies the contents of one string to another.
Syntax: char *strcpy(char *dest, const char *src);
Example:
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("Copied string: %s\n", destination); return 0; }
strcat() - String Concatenation:
strcat() appends a copy of the source string to the destination string.
Syntax: char *strcat(char *dest, const char *src);
Example:
#include <stdio.h> #include <string.h> int main() { char dest[20] = "Hello, "; char src[] = "World!"; strcat(dest, src); printf("Concatenated string: %s\n", dest); return 0; }
strcmp() - String Comparison:
strcmp() compares two strings and returns an integer indicating their lexicographical relationship.
Syntax: int strcmp(const char *str1, const char *str2);
Example:
#include <stdio.h> #include <string.h> int main() { char str1[] = "Hello"; char str2[] = "World"; int result = strcmp(str1, str2); if (result == 0) { printf("Strings are equal.\n"); } else if (result < 0) { printf("String 1 comes before string 2.\n"); } else { printf("String 2 comes before string 1.\n"); } return 0; }
Implementation without using standard library functions
#include <stdio.h> // Custom implementation of strlen() int my_strlen(const char *str) { int length = 0; while (*str != '\0') { length++; str++; } return length; } // Custom implementation of strcpy() void my_strcpy(char *dest, const char *src) { while (*src != '\0') { *dest = *src; src++; dest++; } *dest = '\0'; // Ensure null termination } // Custom implementation of strcat() void my_strcat(char *dest, const char *src) { while (*dest != '\0') { dest++; } while (*src != '\0') { *dest = *src; src++; dest++; } *dest = '\0'; // Ensure null termination } // Custom implementation of strcmp() int my_strcmp(const char *str1, const char *str2) { while (*str1 == *str2) { if (*str1 == '\0') return 0; str1++; str2++; } return (*str1 - *str2); } int main() { // Example usage char str1[ 20 ] = "Hello"; char str2[] = "World"; char str3[ 20 ]; int result; // Print length of str1 using custom strlen() printf("Length of str1: %zu\n", my_strlen(str1)); // Copy str1 to str3 using custom strcpy() my_strcpy(str3, str1); printf("Copied string: %s\n", str3); // Concatenate str2 to str3 using custom strcat() my_strcat(str3, str2); printf("Concatenated string: %s\n", str3); // Compare str1 and str3 using custom strcmp() result = my_strcmp(str1, str3); if (result == 0) { printf("str1 and str3 are equal.\n"); } else if (result < 0) { printf("str1 comes before str3.\n"); } else { printf("str3 comes before str1.\n"); } return 0; }
This example demonstrates the usage of custom implementations of strlen(), strcpy(), strcat(), and strcmp(). It initializes strings, calculates their lengths, copies and concatenates them, and finally compares them, all using the custom functions.
Comments
Post a Comment
write your complements and complaints :)