Apply for Zend Framework Certification Training

c



< 20 Advanced Pointer Questions with Solutions in C Pointer Arithmetic in C >



10 Advanced Coding Problems On String Pointers in C
1. Remove Duplicate Characters (In-Place using Pointers)
Problem:
Remove duplicate characters from a string without using extra array.
Code:
#include <stdio.h>
void removeDuplicates(char *str) {
    char *p1 = str;
    while (*p1) {
        char *p2 = p1 + 1;
        char *p3 = p1 + 1;
        while (*p2) {
            if (*p1 != *p2) {
                *p3 = *p2;
                p3++;
            }
            p2++;
        }
        *p3 = '\0';
        p1++;
    }
}

int main() {
    char str[] = "programming";
    removeDuplicates(str);
    printf("%s", str);
}
Output:
 progamin

2. Check if Two Strings are Anagrams (Using Pointers)
Problem:
Check if two strings are anagrams without using sorting.
Code:
#include <stdio.h>
int isAnagram(char *s1, char *s2) {
    int count[256] = {0};
    while (*s1) count[*s1++]++;
    while (*s2) count[*s2++]--;
    for (int i = 0; i < 256; i++) {
        if (count[i] != 0)
            return 0;
    }
    return 1;
}
int main() {
    char s1[] = "listen";
    char s2[] = "silent";
    if (isAnagram(s1, s2))
        printf("Anagram");
    else
        printf("Not Anagram");
}
Output:
Anagram
3. Longest Word in a String (Pointer Traversal)
Problem:
Find the longest word using only pointers.
Code:
#include <stdio.h>
void longestWord(char *str) {
    int maxLen = 0, currLen = 0;
    char *maxStart, *currStart;
    while (*str) {
        if (*str != ' ') {
            if (currLen == 0)
                currStart = str;
            currLen++;
        } else {
            if (currLen > maxLen) {
                maxLen = currLen;
                maxStart = currStart;
            }
            currLen = 0;
        }
        str++;
    }
    if (currLen > maxLen) {
        maxLen = currLen;
        maxStart = currStart;
    }
    for (int i = 0; i < maxLen; i++)
        printf("%c", *(maxStart + i));
}
int main() {
    char str[] = "C programming is powerful";
    longestWord(str);
}
Output:
Programming
In normal
#include<stdio.h>
int main(){
    char mystring[]="c is an programming language";
    int length=0,count=0,largestWordlength=0,wordIndex=0,i;
    while(mystring[length]!='\0'){
        length++;
    }
    for(i=0;i<=length;i++){
        if(mystring[i]==' '|| mystring[i]=='\0'){
            if(largestWordlength < count){
                largestWordlength = count;
                wordIndex=i;
            }
            printf("%d \n",count);
            count=0;
        }else{
            count++;
        }
    }
    printf("Largest word index  %d and length %d\n",wordIndex,largestWordlength);
    int start= wordIndex-largestWordlength;
    printf("%d \n",start);
    for(i=start;i<wordIndex;i++){
        printf("%c",mystring[i]);
    }
}
Output 
1
2
2
11
8
Largest word index  19 and length 11
8
programming
4. Reverse Words in a Sentence (In-Place)
Problem:
Reverse each word of a sentence using pointers.
Code:
#include <stdio.h>
void reverse(char *start, char *end) {
    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

void reverseWords(char *str) {
    char *start = str;
    while (*str) {
        if (*str == ' ') {
            reverse(start, str - 1);
            start = str + 1;
        }
        str++;
    }
    reverse(start, str - 1);
}
int main() {
    char str[] = "hello world";
    reverseWords(str);
    printf("%s", str);
}
Output:
olleh dlrow
5. Implement strstr() (Substring Search using Pointer)
Problem:
Find first occurrence of substring in a string.
Code:
#include <stdio.h>
char* myStrstr(char *str, char *sub) {
    char *p, *q;
    while (*str) {
        p = str;
        q = sub;
        while (*p && *q && *p == *q) {
            p++;
            q++;
        }
        if (*q == '\0')
            return str;
        str++;
    }
    return NULL;
}
int main() {
    char str[] = "hello world";
    char sub[] = "world";
    char *res = myStrstr(str, sub);
    if (res)
        printf("%s", res);
    else
        printf("Not found");
}
Output:
world
6. Check if String is Rotation of Another
Problem:
Check if s2 is rotation of s1.
Idea:
Concatenate s1 + s1
Check if s2 is substring 
In normal
#include <stdio.h>
#include <string.h>
int isRotation(char str1[], char str2[]) {
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    // Step 1: Length must be equal
    if (len1 != len2)
        return 0;
    // Step 2: Create a new string (str1 + str1)
    char temp[2 * len1 + 1];
    strcpy(temp, str1);
    strcat(temp, str1);
    // Step 3: Check if str2 is substring of temp
    if (strstr(temp, str2) != NULL)
        return 1;
    return 0;
}
int main() {
    char str1[] = "ABCD";
    char str2[] = "CDAB";
    if (isRotation(str1, str2))
        printf("Rotation\n");
    else
        printf("Not Rotation\n");
    return 0;
}
Using pointer
Code:
#include <stdio.h>
#include <string.h>
int isRotation(char *s1, char *s2) {
    char temp[200];
    strcpy(temp, s1);
    strcat(temp, s1);
    return strstr(temp, s2) != NULL;
}
7. Compress String (Run-Length Encoding)
Problem:
Convert "aaabbc" → "a3b2c1"
Code:
 In Normal 
#include<stdio.h>
int main(){
    char str[]="aaaabbbbttx";
    int i,count;
    for(i=0;str[i] != '\0';i++){
        count=1;
        while(str[i] == str[i+1]){
            count++;
            i++;
        }
        printf("%c%d",str[i],count);
    }
}

Using Pointer 
#include <stdio.h>
void compress(char *str) {
    char result[100];
    int i = 0, j = 0;
    while (str[i]) {
        char ch = str[i];
        int count = 0;
        while (str[i] == ch) {
            count++;
            i++;
        }
        result[j++] = ch;
        result[j++] = count + '0';
    }
    result[j] = '\0';
    printf("%s", result);
}
Output:
a3b2c1

In Java 

import java.util.Scanner;
public class RLECompression {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = sc.next();
        for (int i = 0; i < str.length(); i++) {
            int count = 1;
            // Count consecutive characters
            while (i < str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) {
                count++;
                i++;
            }
            // Print compressed output
            System.out.print(str.charAt(i) + "" + count);
        }
        sc.close();
    }
}
8. First Non-Repeating Character
Problem:
Find first non-repeating character using pointers.
Code:
#include <stdio.h>
char firstUnique(char *str) {
    int count[256] = {0};
    char *p = str;
    while (*p)
        count[*p++]++;
    p = str;
    while (*p) {
        if (count[*p] == 1)
            return *p;
        p++;
    }
    return '\0';
}
9. Remove All Occurrences of a Character
Problem:
Remove all 'a' from string using pointers.
In Normal
#include <stdio.h>
void removeChar(char str[], char ch) {
    int i = 0, j = 0;
    while (str[i] != '\0') {
        if (str[i] != ch) {
            str[j] = str[i];
            j++;
        }
        i++;
    }
    str[j] = '\0'; // terminate new string
}
int main() {
    char str[] = "programming";
    char ch = 'g';
    removeChar(str, ch);
    printf("Updated string: %s\n", str);
    return 0;
}
Using Pointer
Code:

void removeChar(char *str, char ch) {
    char *src = str, *dest = str;
    while (*src) {
        if (*src != ch)
            *dest++ = *src;
        src++;
    }
    *dest = '\0';
}
10. Palindrome Check using Pointers
Problem:
Check if string is palindrome using two pointers.
Code:
#include<stdio.h>
int main(){
    char mystring[]= "madixdam";
    char *p = mystring;
    int l=0;
    while(*p != '\0'){
        p++;
        l++;
    }
    char *start = &mystring[0];
    char *end  = &mystring[l-1];
    int ispalendrome=1;
    for(int i=0;i<l/2;i++){
        if(*start != *end){
            ispalendrome =0;
            break;
        }
        start++;
        end--;
    }
    if(ispalendrome ==1){
        printf("Plendrome");
    }else{
        printf("Not Plendrome");
    }
}

In normal 

#include <stdio.h>
#include <string.h>
int main() {
    char str[100];
    int i, length, isPalindrome = 1;
    printf("Enter a string: ");
    scanf("%s", str);
    length = strlen(str);
    for(i = 0; i < length / 2; i++) {
        if(str[i] != str[length - i - 1]) {
            isPalindrome = 0;
            break;
        }
    }
    if(isPalindrome)
        printf("Palindrome\n");
    else
        printf("Not a Palindrome\n");
    return 0;
}

< 20 Advanced Pointer Questions with Solutions in C Pointer Arithmetic in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top