Processing strings using pointers

 

Processing Strings with Pointers in C

Using pointers to manipulate strings in C allows efficient traversal, copying, concatenation, and modification without relying on array indexing.


1. Basics of Strings with Pointers

In C, a string is an array of characters terminated by a null character ('\0'). A pointer can be used to access each character sequentially.

#include <stdio.h> int main() { char str[] = "Hello, World!"; char *p = str; // Pointer to the first character of the string // Print each character using the pointer while (*p != '\0') { printf("%c ", *p); // Dereference to get character p++; // Move to the next character } return 0; }

Explanation:

  • p starts at &str[0].

  • *p gives the character at the current pointer location.

  • p++ moves the pointer to the next character.

  • This prints all characters in the string sequentially.


2. Finding the Length of a String Using Pointers

We can find the length of a string without strlen() by iterating until the null character is reached.

#include <stdio.h> 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; }

Explanation:

  • The pointer p iterates through the string.

  • Each character increments the length counter until '\0' is reached.


3. Copying a String Using Pointers

Pointers can be used to copy a string from one location to another.

#include <stdio.h> void string_copy(char *dest, char *src) { while (*src != '\0') { *dest = *src; // Copy character src++; // Move to next character in source dest++; // Move to next position in destination } *dest = '\0'; // Null-terminate destination } int main() { char src[] = "Copy me!"; char dest[20]; // Ensure large enough to hold source string_copy(dest, src); printf("Copied string: %s\n", dest); return 0; }

Explanation:

  • Characters are copied one by one from src to dest.

  • Both pointers advance after each copy.

  • Null character '\0' is added at the end.


4. Reversing a String Using Pointers

Two pointers can be used to reverse a string in place.

#include <stdio.h> void string_reverse(char *str) { char *start = str; char *end = str; // Move 'end' to the last character while (*(end + 1) != '\0') { end++; } // Swap characters until pointers meet 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; }

Explanation:

  • start points to the first character, end to the last.

  • Swap *start and *end, then move pointers toward each other.

  • Repeat until start >= end.


5. Concatenating Two Strings Using Pointers

To concatenate, move the dest pointer to the end of the first string and copy the second string there.

#include <stdio.h> void string_concat(char *dest, char *src) { // Move dest to end of first string while (*dest != '\0') { dest++; } // Copy src to dest while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; // Null-terminate concatenated string } int main() { char str1[50] = "Hello, "; char str2[] = "World!"; string_concat(str1, str2); printf("Concatenated string: %s\n", str1); return 0; }

Explanation:

  • dest pointer moves to the null terminator of the first string.

  • src is copied character by character to the end of dest.

  • Null character is added at the end to terminate the concatenated string.

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays