C program to remove repeated words in a string or statement.
In this tutorial we will discuss that how to write a simple c program that print a string or statement after removing the repeated words in a string or statement.
In the first we will enter a string or statement in which we will use some repeated words in that string like:-
Hello friends hey friends
After removing repeated words string is:- Hello friends hey
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0;
char str1[100]={0},substr[100][100]={0},c;
clrscr();
printf("\n ENTER A STRING OR SENTENCE:\n ");
gets(str1);
while(str1[k]!='\0')
{
j=0;
while(str1[k]!=' '&&str1[k]!='\0')
{
substr[i][j]=str1[k];
k++;
j++;
}
substr[i][j]='\0';
i++;
if(str1[k]!='\0')
{
k++;
}
}
int len=i;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;)
{
if(strcmp(substr[i],substr[j])==0)
{
for(k=j;k<len;k++)
{
strcpy(substr[k],substr[k+1]);
}
len--;
}
else
{
j++;
}
}
}
printf("\n AFTER REMOVING REPEATED WORDS IN THIS STRING,THE STRING IS:\n ");
for(i=0;i<len;i++)
{
printf("%s ",substr[i]);
}
printf("\n");
getch();
}
In this tutorial we will discuss that how to write a simple c program that print a string or statement after removing the repeated words in a string or statement.
In the first we will enter a string or statement in which we will use some repeated words in that string like:-
Hello friends hey friends
After removing repeated words string is:- Hello friends hey
C program code
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0;
char str1[100]={0},substr[100][100]={0},c;
clrscr();
printf("\n ENTER A STRING OR SENTENCE:\n ");
gets(str1);
while(str1[k]!='\0')
{
j=0;
while(str1[k]!=' '&&str1[k]!='\0')
{
substr[i][j]=str1[k];
k++;
j++;
}
substr[i][j]='\0';
i++;
if(str1[k]!='\0')
{
k++;
}
}
int len=i;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;)
{
if(strcmp(substr[i],substr[j])==0)
{
for(k=j;k<len;k++)
{
strcpy(substr[k],substr[k+1]);
}
len--;
}
else
{
j++;
}
}
}
printf("\n AFTER REMOVING REPEATED WORDS IN THIS STRING,THE STRING IS:\n ");
for(i=0;i<len;i++)
{
printf("%s ",substr[i]);
}
printf("\n");
getch();
}
OUTPUT:
In this program we will enter a string in which we will use repeated words then it remove the repeated words in that string or statement.
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment