Pointers & Strings
In C, strings are arrays of characters ending with '\0'.
Because arrays work closely with addresses, pointers become the most powerful way to handle strings.
How Pointers Work With Strings
A string can be represented in two ways:
- Using a character array
- Using a pointer to char
char s1[] = "Hello"; // Stored in array char *s2 = "Hello"; // Pointer to constant string literal
β s1 can be modified
β s2 cannot modify characters (string literal stored in read-only memory)
Pointer Traversal in Strings
You can move a pointer through a string using increments.
char name[] = "Sourav";
char *p = name;
while(*p != '\0'){
printf("%c ", *p);
p++;
}
Pointer Arithmetic With Strings
p + 1β moves to next characterp + nβ jumps n characters*pβ gives current character
Common PointerβString Operations
- Traverse string
- Count characters
- Compare two strings
- Copy one string to another
- Modify string characters
Example: String Length Using Pointer
#include <stdio.h>
int main(){
char s[] = "WebTech";
char *p = s;
int count = 0;
while(*p){
count++;
p++;
}
printf("Length = %d", count);
return 0;
}
Example: Modify Characters Using Pointer
#include <stdio.h>
int main(){
char s[] = "Sahu";
char *p = s;
p[0] = 'R'; // change first letter
printf("%s", s);
return 0;
}
Pointer vs Array Indexing
s[i]is same as*(s + i)- Pointers give faster string access
- Used in most library functions internally
Pointers & Strings β 10 Examples
1. Print String Using Pointer
#include <stdio.h>
int main() {
char *p = "Hello World";
printf("%s", p);
return 0;
}
2. Access Characters Using Pointer
#include <stdio.h>
int main() {
char name[] = "Sourav";
char *p = name;
printf("%c %c %c", *p, *(p+1), *(p+2));
return 0;
}
3. Modify String Using Pointer
#include <stdio.h>
int main() {
char word[] = "Cat";
char *p = word;
p[0] = 'B';
printf("%s", word); // Bat
return 0;
}
4. Count Length Using Pointer
#include <stdio.h>
int main() {
char s[] = "Pointers";
char *p = s;
int len = 0;
while(*p != '\0') {
len++;
p++;
}
printf("Length: %d", len);
return 0;
}
5. Count Vowels Using Pointer
#include <stdio.h>
int main() {
char s[] = "Education";
char *p = s;
int count = 0;
while(*p) {
char ch = *p;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
count++;
p++;
}
printf("Vowels: %d", count);
return 0;
}
6. Compare Two Strings Using Pointer
#include <stdio.h>
int strcompare(char *a, char *b) {
while(*a && *b) {
if(*a != *b) return 0;
a++; b++;
}
return (*a == '\0' && *b == '\0');
}
int main() {
printf("%d", strcompare("abc", "abc"));
return 0;
}
7. Reverse String Using Pointer
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Sourav";
int i = 0, j = strlen(s) - 1;
char t;
while(i < j) {
t = s[i];
s[i] = s[j];
s[j] = t;
i++; j--;
}
printf("%s", s);
return 0;
}
8. Convert to Uppercase Using Pointer
#include <stdio.h>
int main() {
char s[] = "hello";
char *p = s;
while(*p) {
if(*p >= 'a' && *p <= 'z') {
*p = *p - 32;
}
p++;
}
printf("%s", s);
return 0;
}
9. Copy String Using Pointer
#include <stdio.h>
void copy(char *d, char *s) {
while(*s) {
*d = *s;
d++; s++;
}
*d = '\0';
}
int main() {
char src[] = "India";
char dest[20];
copy(dest, src);
printf("%s", dest);
return 0;
}
10. Find Frequency of Character
#include <stdio.h>
int main() {
char s[] = "programming";
char *p = s;
char ch = 'm';
int count = 0;
while(*p) {
if(*p == ch)
count++;
p++;
}
printf("Frequency: %d", count);
return 0;
}
Where Pointer-String Handling Is Used?
- Text editors
- Input validation
- Token parsing
- Command-line arguments
- File names and paths
Practice Questions
- Explain relation between pointers and strings.
- Why can't we modify string literals?
- How can you traverse a string using a pointer?
- Difference between
char s[]andchar *s? - Write pointer-based string length logic.
Practice Task
Write a C program using a pointer to:
β Count vowels
β Count consonants
β Print characters in reverse
β Count vowels
β Count consonants
β Print characters in reverse