简体   繁体   English

我正在尝试比较字符串文字,我想删除重复的文字,我想在不使用 POINTERS 的情况下进行。,

[英]I am trying to compare string literals and I want to remove repeated literals I want to do it without using POINTERS.,

I am trying to compare string literals and I want to remove repeated literals I want to do it without using POINTERS.我正在尝试比较字符串文字,我想删除重复的文字,我想在不使用 POINTERS 的情况下进行。

This is my code:这是我的代码:

char str[30];

printf("Enter strings : ");     

fgets(str,29,stdin);

char tem[30];

int count , county;

for(count = 0 ; count < strlen(str)-1 ; count++) {      
    for(county = 1 ; county < strlen(str) ; county++) {
        if(str[count] != str[county]) {
               tem[count] = str[count];     
        }
    } 
}

//PRINT

for(count = 0 ;count < strlen(str) -1 ; count++) {

    printf("%c",tem[count]);

}

Input: happen输入: happen
Expected output: hapen预期输出: hapen

Correcting your code:更正您的代码:

#include <stdio.h>
#include <string.h>

int main()
{
    char str[30];

    printf("Enter strings : ");

    fgets(str,30,stdin);

    char tem[30];

    size_t count;
    size_t county=0;;

    for(count = 0 ; count < strlen(str)-1 ; count++) {
            if(str[count] != str[count+1]) {
                   tem[county++] = str[count];
            }
    }

    tem[county] = '\0';

    printf("%s\n", tem);

    return 0;
}

Take note that this code remove double chars if this char have +1 displacement in the sting.请注意,如果此字符在字符串中具有 +1 位移,则此代码将删除双字符。

EDIT编辑

To have mspi as output of entered string mississippimspi作为输入字符串mississippi输出

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main()
{
    char str[30];

    printf("Enter strings : ");

    fgets(str,30,stdin);

    char tem[30];

    size_t count;
    size_t county;
    size_t tem_index = 0;
    size_t size_of_string = strlen(str);
    bool found;

    for(count = 0 ; count < size_of_string-1; count++)
    {
        found = false;
        county = count+1;

        while ((found == false) && (county<size_of_string))
        {
            if(str[count] == str[county])
            {
                found = true;
            }
            county++;
        }

        if (found == false)
        {
            tem[tem_index++] = str[count];
        }
    }

    tem[tem_index] = '\0';

    printf("%s\n", tem);

    return 0;
}

EDIT 2编辑 2

To have misp as output of entered string mississippimisp作为输入字符串mississippi输出

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main()
{
    char str[30];

    printf("Enter strings : ");

    fgets(str,30,stdin);

    char tem[30];

    size_t count;
    size_t county;
    size_t tem_index = 0;
    size_t size_of_string = strlen(str);
    bool found;

    for(count = 0 ; count < size_of_string-1; count++)
    {
        found = false;
        county = 0;

        while ((found == false) && (county<tem_index))
        {
            if(str[count] == tem[county])
            {
                found = true;
            }
            county++;
        }

        if (found == false)
        {
            tem[tem_index++] = str[count];
        }
    }

    tem[tem_index] = '\0';

    printf("%s\n", tem);

    return 0;
}

Take note that all these solutions are case sensitive.请注意,所有这些解决方案都区分大小写。

Your code to remove allduplicates isn't quite there yet:您删除所有重复项的代码还没有完成:

  • You use the same indices for the new and the old string, but you need two different indices, because the new string is as long or shorter than the old string.您对新字符串和旧字符串使用相同的索引,但您需要两个不同的索引,因为新字符串与旧字符串一样长或短。 If the old string is "aaab", your index for the old string is 3 when you see the "b", but the index for the new string is only 1. (By skipping indices you leave uninitialised gaps in your string.)如果旧字符串是“aaab”,则当您看到“b”时,旧字符串的索引为 3,但新字符串的索引仅为 1。(通过跳过索引,您会在字符串中留下未初始化的间隙。)

  • You look forward to find other occurrences of the same letter, but you append to the new string for every letter that doesn't atch.您期待找到同一字母的其他出现,但您将每个不匹配的字母附加到新字符串中。 You must look at all folllowing letters, but you must append to the new string only once.您必须查看所有以下字母,但您必须仅附加到新字符串一次。 That is, you must make your decision whether the letter is duplicate or not after the loop, based on the information that you've found in the loop.也就是说,您必须根据您在循环中找到的信息,决定循环后该字母是否重复。

  • When you look forward, you shouldn't start a 1, but at the letter after he current letter.当你向前看时,你不应该从 1 开始,而是从当前字母之后的字母开始。 If you start at one, you will find duplicates for every letter after the first, because you check each letter with itself.如果你从一个开始,你会发现第一个之后的每个字母都有重复,因为你检查每个字母。

  • This is not an arror, but it's not a good idea to call strlen repeatedly in a loop.这不是错误,但在循环中重复调用strlen不是一个好主意。 The length of the input string doesn't change, so you can determine the string length beforehand.输入字符串的长度不会改变,因此您可以预先确定字符串长度。 If you just want to use it as your termination condition, you can test whethet the current letter is the null terminator.如果您只想将其用作终止条件,则可以测试当前字母是否为空终止符。

Below is a solution that uses your logic, albeit by looking backwards, not forward.下面是一个使用您的逻辑的解决方案,尽管是向后看,而不是向前看。 (If you look forward, you will copy the last occurence of a letter, if you look back, you'll copy the first occurrence. It may make a difference in the order of the letters. For Mississippi, you'll get "Mspi" or "Misp" depending on which strategy you use.) (如果向前看,您将复制一个字母的最后一次出现,如果您向后看,您将复制第一次出现。这可能会使字母顺序有所不同。对于密西西比州,您将得到“Mspi ”或“Misp”取决于您使用的策略。)

The program overwrites the same string.程序会覆盖相同的字符串。 This is possible, because you are filtering out letters and the new index is equal to the old index or smaller:这是可能的,因为您正在过滤字母并且新索引等于旧索引或更小:

#include <stdlib.h>
#include <stdio.h>

void remdup(char *str)
{
    int i = 0;      // index into old string
    int j = 0;      // index into new string

    for (i = 0; str[i]; i++) {
        int k = 0;
        int dup = 0;

        for (k = 0; k < i; k++) {
            if (str[i] == str[k]) {
                dup = 1;
                break;
            }
        }

        if (dup == 0) str[j++] = str[i];
    }

    str[j] = '\0';
}

int main()
{
    char str[] = "Mississippi";

    puts(str);
    remdup(str);
    puts(str);

    return 0;
}

This solution doesn't scale for large strings.此解决方案不适用于大字符串。 A more effective method would be to keep a table of which of the 256 possible characters have already been used.一个更有效的方法是保留一个表格,其中包含 256 个可能的字符中已经使用过的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试通过仅重新排列指针来对链接列表进行冒泡排序。 我的代码有一个要解决的2节点列表的解决方法 - Trying to bubble sort a linked list by only rearranging pointers. My code has a workaround for a 2 node list that I want to get rid of 我是否正确strcmp与文字相同(且安全)? - Am I correct that strcmp is equivalent (and safe) for literals? 从数组转换为指针。 我想念什么吗? - Converting from arrays to pointers. Am I missing something? 我是否误解了有关字符串文字范围的这个示例? - Do I misunderstand this example about scope of string literals? 我应该释放使用字符串文字初始化的 char* 吗? - Should I free char* initialized using string-literals? C 中的指针和字符串文字 - Pointers and string literals in C 我正在尝试使用指针在c中复制字符串 - i am trying to copy a string in c using pointers 如何比较字符串指针? - How do I compare string pointers? 使用C am我认为以多个零开头的文字被认为是八进制的吗? - Using C am I right in thinking that literals beginning with multiple zeros are considered octal? 如何混合使用字符串文字和浮点数并将它们连接成C中的一个字符串? - How do I take a mixture of string literals and floats and concatenate them into one string in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM