STRING
STR, PAT, REP
QUESTION :
Develop a Program in C for the following operations on Strings.
Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don't use Built-in functions.
EXPLANATION :
Here is a C program that performs the operations you've described:
Reads a main string (
STR
)Reads a pattern string (
PAT
)Reads a replace string (
REP
)Replaces all occurrences of
PAT
inSTR
withREP
(ifPAT
exists)Uses functions for each operation
Does not use any built-in string functions like
strcpy
,strlen
, etc.
PROGRAM:
#include <stdio.h>
#define MAX 1000
// Function to calculate length of a string
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
// Function to check if PAT matches STR at position index
int isMatch(char str[], char pat[], int index) {
int i = 0;
while (pat[i] != '\0') {
if (str[index + i] != pat[i])
return 0;
i++;
}
return 1;
}
// Function to copy part of a string from source to destination
void copyString(char dest[], char src[], int *destIndex, int srcStart, int srcEnd) {
for (int i = srcStart; i < srcEnd; i++) {
dest[*destIndex] = src[i];
(*destIndex)++;
}
}
// Function to perform find and replace
void findAndReplace(char str[], char pat[], char rep[], char result[]) {
int i = 0, resIndex = 0;
int found = 0;
int patLen = stringLength(pat);
int strLen = stringLength(str);
while (i < strLen) {
if (isMatch(str, pat, i)) {
// Pattern match found
found = 1;
// Copy replacement string to result
int j = 0;
while (rep[j] != '\0') {
result[resIndex++] = rep[j++];
}
i += patLen; // Skip the pattern
} else {
// Copy current character
result[resIndex++] = str[i++];
}
}
result[resIndex] = '\0'; // Null terminate
if (!found) {
printf("Pattern not found in the main string.\n");
} else {
printf("Resulting String after Replacement: %s\n", result);
}
}
// Main function
int main() {
char str[MAX], pat[MAX], rep[MAX], result[MAX * 2]; // result might be larger
printf("Enter the main string (STR): ");
fgets(str, MAX, stdin);
printf("Enter the pattern string (PAT): ");
fgets(pat, MAX, stdin);
printf("Enter the replacement string (REP): ");
fgets(rep, MAX, stdin);
// Remove newline characters from input if any
int len;
len = stringLength(str);
if (str[len - 1] == '\n') str[len - 1] = '\0';
len = stringLength(pat);
if (pat[len - 1] == '\n') pat[len - 1] = '\0';
len = stringLength(rep);
if (rep[len - 1] == '\n') rep[len - 1] = '\0';
findAndReplace(str, pat, rep, result);
return 0;
}
OUTPUT-1
Replace a string - main string given/matched
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc string.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd
Enter the main string (STR): social work
Enter the pattern string (PAT): social
Enter the replacement string (REP): hard
Resulting String after Replacement: hard work
----------------------------------------------------------------------------------------------------------
OUTPUT-2
Replace a string - main string not matched
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc string.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd
Enter the main string (STR): thank you
Enter the pattern string (PAT): good
Enter the replacement string (REP): how
Pattern not found in the main string.
No comments:
Post a Comment