需要写一个C程序去除相邻的重复字符
Need to write a C Program to remove repeated characters adjacent to each other
我只需要删除彼此相邻的重复字符。
示例:如果输入为 "heeellooo wooorllldd"
,则输出应为 "helo world"
。我目前得到的输出是 "helo wrd"
.
这是我的代码。
#include <stdio.h>
#include <string.h>
main()
{
char str[]="heeello wooorld";
redundant(str);
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check])
{
ch = str[check];
i = j = check + 1;
while(str[i])
{
if(str[i] != ch)
{
str[j] = str[i];
j++;
}
i++;
}
str[j]='[=10=]';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
发生的事情是你在代码中获取了一个字符,然后检查整个字符串是否再次出现相同的字符。如果存在,则将其删除。因此你的程序只有每个字符的一个副本,而不是删除相邻的相同字符。
试试这个代码:
#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <conio.h>
void redundant(char *);
main()
{
clrscr();
char str[]="heeello wooorld";
redundant(str);
getch();
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check]) {
j=i=check;
ch= str[check+1];
if(str[check] == ch)
{
i++;
check--;
}
while(str[i]) {
str[j] = str[i];
j++;
i++;
}
str[j]='[=10=]';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
在我的代码中,我检查相邻字符是否相同,或者 not.If 是的,我从下一个位置复制整个字符串。
您可以使用 strcat 函数缩短代码,如下所示:
void redundant(char *str)
{
int check=0;
while(str[check]) {
if(str[check] == str[check+1])
{
str[check+1]='[=11=]';
strcat(str,str+check+2);
check--;
}
check++;
}
我正在寻找一个简约的解决方案。纯属娱乐。
void redundant(char *str) {
int lastch = -1; /* last read character */
char* inpp = str; /* pointer to input location */
char* outp = str; /* pointer to output location */
while (*inpp != '[=10=]') {
if (*inpp != lastch) {
*outp++ = lastch = *inpp;
}
inpp++;
}
*outp = '[=10=]';
printf("String after removing duplicates : %s\n", str);
}
我只需要删除彼此相邻的重复字符。
示例:如果输入为 "heeellooo wooorllldd"
,则输出应为 "helo world"
。我目前得到的输出是 "helo wrd"
.
这是我的代码。
#include <stdio.h>
#include <string.h>
main()
{
char str[]="heeello wooorld";
redundant(str);
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check])
{
ch = str[check];
i = j = check + 1;
while(str[i])
{
if(str[i] != ch)
{
str[j] = str[i];
j++;
}
i++;
}
str[j]='[=10=]';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
发生的事情是你在代码中获取了一个字符,然后检查整个字符串是否再次出现相同的字符。如果存在,则将其删除。因此你的程序只有每个字符的一个副本,而不是删除相邻的相同字符。
试试这个代码:
#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <conio.h>
void redundant(char *);
main()
{
clrscr();
char str[]="heeello wooorld";
redundant(str);
getch();
}
void redundant(char *str)
{
int check=0;
int i,j;
char ch;
while(str[check]) {
j=i=check;
ch= str[check+1];
if(str[check] == ch)
{
i++;
check--;
}
while(str[i]) {
str[j] = str[i];
j++;
i++;
}
str[j]='[=10=]';
check++;
}
printf("String after removing duplicates : %s\n",str);
}
在我的代码中,我检查相邻字符是否相同,或者 not.If 是的,我从下一个位置复制整个字符串。
您可以使用 strcat 函数缩短代码,如下所示:
void redundant(char *str)
{
int check=0;
while(str[check]) {
if(str[check] == str[check+1])
{
str[check+1]='[=11=]';
strcat(str,str+check+2);
check--;
}
check++;
}
我正在寻找一个简约的解决方案。纯属娱乐。
void redundant(char *str) {
int lastch = -1; /* last read character */
char* inpp = str; /* pointer to input location */
char* outp = str; /* pointer to output location */
while (*inpp != '[=10=]') {
if (*inpp != lastch) {
*outp++ = lastch = *inpp;
}
inpp++;
}
*outp = '[=10=]';
printf("String after removing duplicates : %s\n", str);
}