Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C Program to Remove Duplicate Characters from a String
๐งน C Program to Remove Duplicate Characters from a String
#include <stdio.h>
#include <string.h>
int main() {
int i, j, k;
char str[100];
printf("Enter the string:\n");
scanf(" %[^\n]", str); // space before %[^\n] to handle newline
for (i = 0; str[i] != '\0'; i++) {
j = i + 1;
while (str[j] != '\0') {
if (str[j] == str[i]) {
// Shift all characters one position to the left
for (k = j; str[k] != '\0'; k++) {
str[k] = str[k + 1];
}
// Don't increment j here — next character is already shifted
} else {
j++;
}
}
}
printf("After removing Duplicate Elements in Given String: %s\n", str);
return 0;
}
๐ Explanation:
This program reads a string from the user and removes duplicate characters by shifting the remaining characters left whenever a duplicate is found.
๐ก Sample Output:
Enter the string: programming After removing Duplicate Elements in Given String: progamin
๐ Keywords:
remove duplicates from string in C, string manipulation in C, C string interview programs, delete repeated characters, remove duplicate characters in C
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment