C程序用于删除字符串中的重复字符...显示运行时错误 [英] C program for removing duplicate characters in a string...Shows run time error

查看:138
本文介绍了C程序用于删除字符串中的重复字符...显示运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下函数来从字符串中删除重复的字符。例如:如果
str =heeello;
removeDuplicate(str)



将返回helo ...但它在运行时显示一些错误。我添加了一些printf()语句进行调试...任何人都可以告诉我问题是什么?

  char * removeDuplicate(char str [])//从字符串中删除重复的字符,以便字符串中的每个字符不重复
{
int i = 0,j;
char ch;
printf(\\\
str is%s,str);
while((ch = str [i ++])!='\\ \\
{
j = i;
printf(\\\
---- ch =%c ----,ch);
while(str [j]!='\0')
{
printf(\\\
--------检查%c =%c \\\
,str [j] ,ch);
if(ch == str [j])
{
printf(\\\
------------是);
while(str [j]!='\0')
{
printf(\\\
Removing%c%d - \\\
,str [j]);
str [j] = str [++ j];
--i

}

break;
}
printf(\\\
------------否);

// printf(\\\
jj);
j ++;
}
}

return str;
}


解决方案

你传递一个字符串文字,您不能修改此功能,而应该执行以下操作:

  char myStr [] =heee; 
removeDuplicate(myStr);

另请注意,在以下行中,您必须在printf(%c%d ),但只传递一个参数( str [j] ):

  printf(\\\
Removing%c%d - \\\
,str [j]);

这可能会导致各种不良事情...


I wrote the following function for removing duplicate characters from a string..For ex: if str = "heeello; removeDuplicate(str)

will return helo...But it shows some error on runtime .I have added some printf() statements for debugging...Can anyone tell me what the problem is ?

char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in                a string is not repeating  
{
    int i = 0,j;
    char ch;
    printf("\nstr is %s",str);
    while((ch = str[i++] )!= '\0')
    {
    j = i;
    printf("\n----ch = %c----",ch);
    while(str[j] != '\0')
    {
        printf("\n--------Checking whether %c =  %c \n",str[j],ch);
        if(ch == str[j])
        {
            printf("\n------------Yes");
            while(str[j]!='\0')
            { 
                printf("\nRemoving %c %d -- \n",str[j]);
                str[j] = str[++j];
                --i;

            }

            break;
        }
        printf("\n------------No");

       //printf("\njj");
        j++;
    }
}

return str;
}

解决方案

You are passing a string literal, which you are not allowed to modify to this function, instead you should do:

char myStr[] = "heee";
removeDuplicate(myStr);

Also, please note that in the following lines your have to specifiers inside the printf (%c %d), but you pass only one argument (str[j]):

printf("\nRemoving %c %d -- \n",str[j]);

This may cause all sorts of bad things...

这篇关于C程序用于删除字符串中的重复字符...显示运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆