Processing strings using pointers
Processing strings with pointers in C is a powerful way to manipulate and traverse strings efficiently. Let’s go through some examples step-by-step to understand how pointers work with strings in C.
1. Basics of String with Pointers
In C, a string is an array of characters terminated by a null character ('\0'). When we declare a string, we can use a pointer to access each character.
int main() {
char str[] = "Hello, World!";
char *p = str; // Pointer to the first character of the string
// Print each character in the string using the pointer
while (*p != '\0') {
printf("%c ", *p); // Dereference to get the character
p++; // Move to the next character
}
return 0;
}
In this example:
pstarts at the address ofstr[0].*pgives the character at the current position.p++advances the pointer to the next character in the string.
This prints each character in str one by one.
2. Finding the Length of a String Using Pointers
We can find the length of a string without using strlen by moving a pointer until it reaches the null character.
int string_length(char *p) {
int length = 0;
while (*p != '\0') {
length++;
p++; // Move to the next character
}
return length;
}
int main() {
char str[] = "Pointer Example";
printf("Length of '%s' is: %d\n", str, string_length(str));
return 0;
}
p iterates through the string until *p is '\0', counting each character to determine the length.3. Copying a String Using Pointers
Let's copy one string into another using pointers. We'll use src to point to the source string and dest for the destination.
void string_copy(char *dest, char *src) {
while (*src != '\0') {
*dest = *src; // Copy character from src to dest
src++; // Move to the next character in src
dest++; // Move to the next position in dest
}
*dest = '\0'; // Null-terminate the destination string
}
int main() {
char src[] = "Copy me!";
char dest[20]; // Ensure this is large enough to hold src
string_copy(dest, src);
printf("Copied string: %s\n", dest);
return 0;
}
In this example:
*dest = *src;copies the character fromsrctodest.- After copying, both pointers are moved forward with
src++anddest++. - Finally, we add a null character at the end of
destto terminate the string.
4. Reversing a String Using Pointers
To reverse a string in place, we can use two pointers: one at the beginning and one at the end. They swap characters and move toward each other.
#include <stdio.h>void string_reverse(char *str) {
char *start = str;
char *end = str;
// Move 'end' pointer to the last character
while (*(end + 1) != '\0') {
end++;
}
// Swap characters from start and end until they meet in the middle
while (start < end) {
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char str[] = "Reverse me!";
string_reverse(str);
printf("Reversed string: %s\n", str);
return 0;
}
In this function:
startandendinitially point to the beginning and end of the string, respectively.- We swap
*startand*end, then incrementstartand decrementenduntil they cross each other.
5. Concatenating Two Strings Using Pointers
To concatenate two strings, we find the end of the first string and then start copying the second string from that point.
#include <stdio.h>void string_concat(char *dest, char *src) {
// Move dest pointer to the end of the first string
while (*dest != '\0') {
dest++;
}
// Copy src to the end of dest
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0'; // Null-terminate the concatenated string
}
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
string_concat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Here:
- The pointer
destmoves to the end of the first string. - Then it starts copying each character from
srcuntil'\0'is reached. - We end by adding a null character to complete the concatenation.
Comments
Post a Comment