简体   繁体   English

从C中的数组中删除空格?

[英]Remove spaces from an array in C?

I am trying to remove the spaces from my array "secuencia", the users give me this entry: 我试图从我的数组“ secuencia”中删除空格,用户给了我这个条目:

"1 2 3 4 5 6 7 8 9" “ 1 2 3 4 5 6 7 8 9”

I want to remove the spaces, and save it in another array for later. 我想删除空格,并将其保存在另一个数组中以备后用。 Then, convert to integer with "ATOI" like I do with the arrays "palancas" and "palancaroja". 然后,像我对数组“ palancas”和“ palancaroja”所做的那样,用“ ATOI”转换为整数。 Those two arrays only contained one number, so I had no problem with them. 那两个数组只包含一个数字,所以我对它们没有问题。

please help me... I am programming in ANSI C. 请帮助我...我正在用ANSI C编程。

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

int main(int argc, const char * argv[])
{
    char palancas [20000];
    int palancai;
    char palancaroja[10];
    int palancarojai;
    char secuencia[20000];
    char temp[20000];
    int j = 0;

    printf("Dame El Numero De Palancas:");
    fgets(palancas, 20000, stdin);

    printf("Dame La Posision De La Palanca Roja:");
    fgets(palancaroja, 10, stdin);

    palancai = atoi(palancas);
    palancarojai = atoi(palancaroja);

    printf("Dame La cadena");
    fgets(secuencia, 20000, stdin);

    for (int i = 0; i < palancai; i++) {

        if (secuencia [i] != ' ') {

        temp [i] = secuencia [i];


            printf("%s", temp);


        }
    }
}

This is the simplest way to remove spaces from a string. 这是从字符串中删除空格的最简单方法。

char *SourcePtr = secuencia;
char *TargetPtr = SourcePtr;

while (*SourcePtr != 0)
{
    if (*SourcePtr != ' ')
    {
       *TargetPtr = *SourcePtr;
       TargetPtr += 1;
    }
    SourcePtr += 1;
}
*TargetPtr = 0;

Translated version of critical section 关键部分的翻译版本

for (int i = 0; i < length; i++) {
    if (source[i] != ' ') {
        temp[i] = source[i];
        printf("%s", temp);
    }
}

This code copies every character from the array source to the array temp , but simply skips spaces. 此代码将每个字符从数组source复制到数组temp ,但仅跳过空格。 So if temp is initialized with XXXXX and source is ABC , then temp is AXBXC after the execution of the loop. 因此,如果使用XXXXX初始化了temp ,并且sourceABC ,那么在执行循环之后, tempAXBXC

You have use two indexes (see other answer) 您使用了两个索引(请参阅其他答案)

#include <stdio.h>

//copy to d from s removed space
void remove_space(char *d, const char *s){
    for(;*s;++s){
        if(*s != ' ')
            *d++ = *s;
    }
    *d = *s;
}

int main(){//DEMO
    char secuencia[] = "1 2 3 4 5 6 7 8 9";
    char temp[sizeof(secuencia)];
    remove_space(temp, secuencia);
    puts(temp);//123456789
    return 0;
}

You could use strtok and tokenize the string that you get with the delimiter string being " ". 您可以使用strtok并用定界符字符串为“”标记所获得的字符串。

In other words: 换一种说法:

char * tok;
int i = 0;
tok = strtok(secuencia, " ");
while(tok != NULL){
    temp[i] = tok[0];
    i++;
    tok = strtok(NULL, " ");
}

This would only work if it's guaranteed that it's a single digit between each space though. 但是,只有在保证每个空格之间只有一位数字的情况下,这才起作用。 Another way to copy it would be to use another loop, cycling through strtok until '\\0' is reached, or using strcpy. 复制它的另一种方法是使用另一个循环,循环遍历strtok直到到达'\\ 0'或使用strcpy。

first I think that your for loop is looking at the wrong variable. 首先,我认为您的for循环正在查看错误的变量。 you are trying to loop on palancai where really you want to loop on secuencia. 您正在尝试在palancai上循环播放,而实际上您想在secuencia循环播放。

Below you can find a function that will parse your int. 在下面,您可以找到一个可以解析您的int的函数。

int MyIntParse(char* str)
{
    int iReturn = 0;
    for(int i=0;i<20000;++i)
    {
        iReturn *=10;
        switch(str[i])
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
                iReturn = iReturn + (str[i] - '0');
                break;
        }
    }
    return iReturn;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM