69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
int main()
{
char s[255], d[255];
int i, j, k = 0, len;
char ch;
int flag = 0; /*增加标志标量, 初始化为0*/
scanf("%s", s);
len = strlen(s);
while(!flag) {
len = strlen(s);
for(i = 0; i < len; i = j) { /*对字符串s进行遍历*/
ch = s[i];
/*分别拿s[i]后面的字符与s[i]进行比较, 如果相等, 则移向下一个字符进行比较.*/
for(j = i + 1; ch == s[j] && j < len; j++);
/*如果参与比较的字符个数小于2, 则说明相邻元素间不存在相同元素,加入目标数组*/
if(j - i < 2) {
d[k++] = ch;
}
}
d[k] = '\0';
/*源串s与目标串d长度比较, 如果不相等,说明该趟去重中有重复字符,再进行一次去重*/
/*直到源串和目的串的长度相等*/
if(strlen(d) != len) {
strcpy(s, d);
k = 0;
}
else { /*源串与目的串长度相等, 说明目的串中相邻元素间已经没有重复字符*/
flag = 1; /*置标志, 退出循环*/
}
}
printf("%s\n", d);
return 0;
}
void foo(char * s, char * result)
{
char * base = result;
char * top = result;
char c;
int l = strlen(s);
int i;
int dup = 0;
for (i = 0; i <= l; i++)
{
c = s[i];
if (base == top)
{
*top++ = c;
continue;
}
if(c == *(top - 1))
{
*top++ = c;
dup += 1;//dup采用自加的方式记录重复次数
continue;
}
if(c != *(top - 1) && dup)
{
top=top-dup-1;//top直接移动,代替while循环
if (c == *(top - 1))
dup = 1;
else
dup = 0;
*top++ = c;
continue;
}
if (c != *(top - 1) && !dup)
{
*top++ = c;
continue;
}
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
char buff[200];
int i,j;
char oldc,oldc1;
char dest[200];
oldc=oldc1=0;
scanf("%s",buff);
j=0;
for(i=0;i<strlen(buff);i++)
{
if (buff[i]!=oldc)
{
oldc=buff[i];
if (j)
{
if(dest[j-1]==oldc)
{
j--;
continue;
}
}
dest[j++]=oldc;
}
else
{
if (oldc!=oldc1)
{
j--;
oldc1=oldc;
}
}
}
dest[j]=0;
printf("%s\n",dest);
system("pause");
}
int caonima(char *s)
{
if(s==NULL)
return 0;
int i=0;
while(s[i]!=0)
{
s[i]=s[i+1];
i++;
}
return 1;
}
int rinima(char *s)
{
if(s==NULL)
return 0;
char *p=s;
while(*p!=0)
{
if(*p==*(p+1))
{
caonima(p);
}
else
p++;
}
return 1;
}
int main(int argc, char* argv[])
{
char s[]="absfajkdcsssnjaaaa";
rinima((char*)s);
puts(s);
return 0;
}
$ ./x abc
3: abc
abc
$ ./x aabbcc
6: aabbcc
$ ./x abbacc
6: abbacc
$ ./x abccba
6: abccba
$ ./x abbbac
6: abbbac
c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
void foo(char * s, char * result) // result为栈结构
{
char * base = result; // 栈底
char * top = result; // 栈顶
char c, popc; // c为当前字符,popc为弹出字符
int l = strlen(s); // 源字串长度
int i; // 循环用索引
int dup = 0; // 是否有重复标志位
for (i = 0; i <= l; i++) // 注意这里的<=,元字串中的'\0'也需要用来做压栈出栈的处理
{ // 以保证末尾有连续字符能够被正确处理(abcdd这样的字符)
// 得到当前字符
c = s[i]; // why l, i.e the \0 here, to make the stack complete
if (base == top) // 空栈,push c
{
*top++ = c;
continue;
}
if (c == *(top - 1)) // c等于栈顶,push c,置dup状态
{
*top++ = c;
dup = 1;
continue;
}
if (c != *(top - 1) && dup) // c不等于栈顶且dup置位,说明前面有重复字符
{
do {
popc = *--top;
} while (popc == *(top - 1)); // 这里先pop出重复的字符
if (c == *(top - 1)) // 如果c和新的栈顶相同,说明上述第三种情况出现
dup = 1; // 还是置dup状态
else
dup = 0;
*top++ = c;
continue;
}
if (c != *(top - 1) && !dup) // c不等于栈顶且没有dup状态,push c
{
*top++ = c;
continue;
}
}
}
int main(int argc, char * argv[])
{
if (argc != 2)
{
printf("Usage: %s \"a string\"\n", argv[0]);
exit(1);
}
{
int length = strlen(argv[1]);
char * result = (char *)calloc(length + 1, sizeof(char));
if (!result)
{
printf("allocation fail!\n");
exit(1);
}
printf("%d: %s\n", length, argv[1]);
foo(argv[1], result);
printf("%s\n", result);
free(result);
}
return 0;
}