Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain Squeeze Function C language
The squeeze() function in C is a user-defined function that removes all characters from the first string that are present in the second string. This function is particularly useful for filtering characters based on a reference string.
Syntax
void squeeze(char string1[], char string2[]);
Parameters
- string1[] − The source string from which characters will be removed
- string2[] − The reference string containing characters to be removed from string1
How It Works
The squeeze function works by scanning each character in the first string and checking if it exists in the second string. If a character is not found in the second string, it is kept; otherwise, it is removed from the first string.
Example
Here's a complete implementation of the squeeze function −
#include <stdio.h>
void squeeze(char string1[], char string2[]);
int main() {
char string1[50];
char string2[30];
printf("Enter the first string: ");
scanf("%s", string1);
printf("Enter the second string: ");
scanf("%s", string2);
printf("Original string: %s<br>", string1);
squeeze(string1, string2);
printf("Final string: %s<br>", string1);
return 0;
}
void squeeze(char string1[], char string2[]) {
int i, j, k;
for (i = k = 0; string1[i] != '\0'; i++) {
for (j = 0; string2[j] != '\0' && string2[j] != string1[i]; j++)
;
if (string2[j] == '\0')
string1[k++] = string1[i];
}
string1[k] = '\0';
}
Enter the first string: TutorialsPoint Enter the second string: aeiou Original string: TutorialsPoint Final string: TtrsPnt
Key Points
- The function modifies the first string in-place, reducing memory usage
- The algorithm has O(n*m) time complexity where n and m are string lengths
- Characters are removed based on exact character matching
- The function preserves the order of remaining characters
Conclusion
The squeeze function provides an efficient way to filter characters from a string based on a reference pattern. It's commonly used in text processing applications where specific characters need to be removed.
