1.删除字符串中重复的字符 2.删除字符串中指定的字符串 3.删除数组中重复元素

时间:2021-12-01 22:16:22
#include<stdio.h>
#include<string.h>
void del_1(char s[]) //删除重复字符
{
	int i, j, k;
	for(i=0; s[i] != '\0'; i++)
		for(j=i+1; s[j] != '\0'; )
			if(s[i] == s[j])
			{
				for(k=j; s[k] != '\0'; k++)
					s[k] = s[k+1];
			}
			else
				j++;
			printf("\nDelete duplicate the string of the characters\n\n");
			puts(s);
}

void del_2(char s1[])//删除指定字符串
{
	char a[20];
	int i, j, k, n;
	printf("\nplease input a string your want to delete \n\n");
	gets(a);
	n = strlen(a);
	for(i=0; s1[i] != '\0'; )
	{
		for(k=i, j=0; s1[k] == a[j] && s1[k] != '\0' && a[j] != '\0';  k++, j++);
		if(k-i==n)
		{
				
		for(k=i; s1[k] != '\0'; k++)
		
			s1[k] = s1[k+n];
			
		}
  	    else
		i++;
	}
	printf("\nAfter delete the appoint string output new string\n\n");
	puts(s1);
}


int main()
{
	char s[100], s1[100];
	printf("\nPlease input a string\n\n");
	gets(s);	
	del_1(s);

	printf("\nPlease input a string\n\n");
	gets(s1);
	del_2(s1);

	return 0;
}


#include<stdio.h> //删除数组中的重复元素
void del(int a[], int n)
{
	int i, j, k;
	for(i=0; i<n; i++)
	{
		for(j=i+1; j<n; )
			if(a[i] == a[j])
			{
				for(k=j; k<n-1; k++)
					a[k] = a[k+1];
				n--;
			}
			else
				j++;
	}

			printf("\n");
			for(i=0; i<n; i++)
		printf("%d ",a[i]);

}
int main()
{
	int a[100];
	int n, i;
	printf("Input the numbers of array: ");
	scanf("%d", &n);

	printf("\nPlease input %d numbers\n", n);
	for(i=0; i<n; i++)
	scanf("%d", &a[i]);
	del(a, n);
}