Mastering C Strings: Memory Manipulation and Pointer Arithmetic – (Part 2)

Table of Contents

Modifying Strings

Accessing string elements refers to retrieving or modifying individual characters within a string by using their index position. In C, since strings are arrays of characters, each character is stored in a contiguous memory location starting from index 0.

Syntax:

string_name[index]

				
					#include <stdio.h>
int main() {
    char str[] = "Hello";
    str[0] = 'Y'; // Valid: Modifying the array
    
    printf("Result 1: %s\n", str); // Output: Yello
    return 0;
}

// Result 1: Yello

				
			

Pointer and String

When you declare a string using a pointer, the string literal is stored in a read-only section of memory, and the pointer holds the memory address of the first character.

Syntax:

Char * ptr = “string_literal”;

				
					#include <stdio.h>
int main() {
    char *p = "C String"; // p points to C
    printf(" %c", *(p + 2)); 
    return 0;
}

// S

				
			

The code defines a pointer that stores the address of the first letter in the string “C String”. By using pointer arithmetic, *(p + 2) moves two positions forward from the start—skipping the letter ‘C’ and the space—to access and print the character at the third position, which is ‘S’.

IndexAddressCharacter
0p + 0'C'
1p + 1' ' (Space)
2p + 2'S'
3p + 3't'
4p + 4'r'

Passing Strings to Functions

Explanation: When a string is declared as a parameter using square brackets, the function signals that it expects a sequence of characters, though internally the compiler treats this as a pointer to the first element.

Syntax:

void function_name(char str[]){…}

Method 2: Using Pointer Notation

Explanation: This method explicitly uses a pointer variable to receive the starting memory address of the string, providing a direct way to navigate the string using pointer arithmetic.

Syntax:

void function_name(char * str){…}

				
					#include <stdio.h>

// Method 1: Using Character Array Notation
void displayUsingArray(char str[]) {
    printf("Array Notation: %s\n", str);
}

// Method 2: Using Pointer Notation
void displayUsingPointer(char *ptr) {
    printf("Pointer Notation: %s\n", ptr);
}

int main() {
    char myString[] = "Hello Qubrica";

    // Calling both functions with the same string
    displayUsingArray(myString);
    displayUsingPointer(myString);

    return 0;
}

// Array Notation: Hello Qubrica
// Pointer Notation: Hello Qubrica

				
			

Write a C program to convert a string to uppercase and then reverse it.

				
					#include <stdio.h>
#include <string.h>
#include <ctype.h>

// Application 1: Convert to Uppercase using Array Notation
void toUpperCase(char str[]) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

// Application 2: Reverse String using Pointer Notation
void reverseString(char *ptr) {
    int len = strlen(ptr);
    char *start = ptr;
    char *end = ptr + len - 1;
    char temp;

    while (start < end) {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}
int main() {
    char message[] = "program";
    // Call Method 1
    toUpperCase(message);
    printf("Uppercase: %s\n", message);
    // Call Method 2
    reverseString(message);
    printf("Reversed:  %s\n", message);

    return 0;
}

// Uppercase: PROGRAM
// Reversed:  MARGORP

				
			
Index (i)str[i] (Before)toupper() Actionstr[i] (After)Resulting String
0'p''p' -> 'P''P'Program
1'r''r' -> R'R'PRogram
2'o''o' -> O'O'PROgram
3'g''g' -> G'G'PROGram
4'r''r' -> R'R'PROGRam
5'a''a' -> A'A'PROGRAm
6'm''m' -> M'M'PROGRAM

Write a c program to check if the entered string is a palindrome.

				
					#include <stdio.h>
#include <string.h>
int main() {
    char s[] = "radar";
    int len = strlen(s);
    int flag = 1;
    for(int i = 0; i < len/2; i++) {
        if(s[i] != s[len-i-1]) {
            flag = 0;
            break;
        }
    }
    if(flag) printf("It is a Palindrome.");
    else printf("Not a Palindrome.");
    return 0;
}

// It is a Palindrome.
				
			
Iteration (i)s[i]s[len-i-1]Comparison (!=)Actionflag
i =0'r''r''r' != 'r' (False)Continue1
i = 1'a''a''a' != 'a' (False)Continue1
End---Loop finished1 (True)