Pointers using String Processing in C++

 Pointers using String Processing 

A string (character array) is actually a sequence of characters stored in contiguous memory locations, ending with a null character '\0'.
A pointer can be used to access and manipulate these characters directly.


 

Basics of String Representation

char str[] = "Hello";

Internally:

Index

Character

Memory Address

0

H

1000

1

e

1001

2

l

1002

3

l

1003

4

o

1004

5

\0

1005

 

Also write:

char *str = "Hello";

 

Pointer and Character Array Relationship

char str[] = "World";

char *ptr = str;    // ptr points to the first character 'W.'

 

cout << *ptr;       // prints 'W'

cout << *(ptr + 1); // prints 'o'

 

So,

  • ptr points to the start of the string.
  • (ptr + i) points to the i-th character.
  • *(ptr + i) gives the character value.

 

Traversing a String using a Pointer

#include <iostream>

using namespace std;

 

int main() {

    char str[] = "SJKPGM";

    char *p = str;

 

    while (*p != '\0') {

        cout << *p;   // print the character

        p++;          // move pointer to next char

    }

    return 0;

}

 

Output:

SJKPGM

---------------------------------------------------------------

Common String Operations Using Pointers

(a) Find String Length

#include <iostream>

using namespace std;

 

int stringLength(char *s) {

    int count = 0;

    while (*s != '\0') {

        count++;

        s++;

    }

    return count;

}

 

int main() {

    char str[] = "HelloWorld";

    cout << "Length = " << stringLength(str);

}

 

Output: Length = 10


(b) Copy One String to Another

void stringCopy(char *src, char *dest) {

    while (*src != '\0') {

        *dest = *src;

        src++;

        dest++;

    }

    *dest = '\0'; // terminate destination string

}

 

int main() {

    char src[] = "SJKPGM";

    char dest[20];

    stringCopy(src, dest);

    cout << "Copied String: " << dest;

}

 

Output: Copied String: SJKPGM

 

(c) Compare Two Strings

int stringCompare(char *s1, char *s2) {

    while (*s1 && (*s1 == *s2)) {

        s1++;

        s2++;

    }

    return *s1 - *s2;

}

 

int main() {

    char a[] = "Apple";

    char b[] = "Applz";

 

    if (stringCompare(a, b) == 0)

        cout << "Equal";

    else

        cout << "Not Equal";

}

 

Output: Not Equal


(d) Reverse a String

 

#include <iostream>

#include <cstring>

using namespace std;

 

void reverseString(char *s) {

    char *start = s;

    char *end = s + strlen(s) - 1;

    char temp;

 

    while (start < end) {

        temp = *start;

        *start = *end;

        *end = temp;

        start++;

        end--;

    }

}

 

int main() {

    char str[] = "Pointer";

    reverseString(str);

    cout << "Reversed String: " << str;

}

 

Output: Reversed String: retnioP


Pointer Arithmetic Summary

If p is a pointer to char:

Operation

Meaning

p + i

Move forward i characters

p - i

Move backward i characters

*p

Value of current character

*(p+i)

i-th character ahead

p++

Move to next character

 

Example: Count Vowels using Pointers

#include <iostream>

using namespace std;

 

int countVowels(char *s) {

    int count = 0;

    while (*s != '\0') {

        char c = tolower(*s);

        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')

            count++;

        s++;

    }

    return count;

}

 

int main() {

    char str[] = "Pointer Using String";

    cout << "Vowel Count: " << countVowels(str);

}

 

 Output: Vowel Count: 6

======================================================================== 

Post a Comment

0 Comments