AIM: Implement various text processing problems
Program 1
PROBLEM Write a program to delete all repeated words in string.
STATEMENT:
ALGORITHM: 1. Declare character arrays for storing input string (str[]),
2. Declare variables i, j, k, l
3. Initialize all the variables to 0
4. Ask user to input a string
5. Read the string along with whitespace
6. Convert the string into 2D array
7. Read each element of the 2D array character by character
8. Compare elements to check whether they are the same or not
9. If they are the same, remove the second same element
10. Print the final elements of the 2D array in form of a string
PROGRAM: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str[100], word[100], new[10][30];
int i=0, j=0, k=0, len1=0, len2=0, l=0;
printf("Enter the string\n");
gets(str);
//convert the string into 2D array
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
new[k][j] = '\0';
k++;
j = 0;
}
else
{
new[k][j] = str[i];
j++;
}
new[k][j] = '\0';
j = 0;
for(i=0;i<k;i++)
{
int present = 0;
for(l=1;l<k+1;l++)
{
if(new[l][j]=='\0' || l==i)
{
continue;
}
if(strcmp(new[i],new[l])==0)
{
new[l][j] = '\0';
present = present + 1;
}
}
j = 0;
for(i=0;i<k+1;i++)
{
if(new[i][j]=='\0')
continue;
else
printf("%s ",new[i]);
}
printf("\n");
return 0;
}
RESULT:
INPUT/ OUTPUT: INPUT:
Welcome to C Programming Class, Welcome to C again!
OUTPUT:
Welcome to C Programming Class, again!
Program 2
PROBLEM Write a program to find and replace a particular word from the string.
STATEMENT:
ALGORITHM: 1. Declare character arrays for input string, word to find and word to
replace
2. Ask the user to input string
3. Read string along with whitespaces
4. Ask user to input word to find
5. Read the word to find
6. Ask user to input word to replace
7. Read word to replace
PROGRAM: #include<stdio.h>
#include <string.h>
#include<stdbool.h>
int main()
{
char str[100], find[100], replace[100];
printf("Enter Input string: ");
fgets(str, 99, stdin);
printf("Word to Find: ");
scanf("%s", &find);
printf("Word to Replace: ");
scanf("%s", &replace);
char del[] = " ";
char *ptr[50];
ptr[0] = strtok(str, del);
int i = 0;
while(ptr[i] != NULL)
{
i++;
ptr[i] = strtok(NULL, del);
}
i = 0;
while(ptr[i] != NULL)
{
if(*ptr[i] == *find)
{
ptr[i] = replace;
}
printf("%s ", ptr[i]);
i++;
}
}
RESULT: