当我将一个字符串绑定到另一个字符串时,有一个空格

时间:2017-12-06 13:29:19

标签: c console-application dev-c++

我班上有一种逻辑作业。所以我的问题是当我尝试将一个字符串strcpy()转换为另一个字符串时,我的新字符串中有一个(就像空格一样)。我不知道如何删除它,也许是我的错误。请帮帮我,谢谢。

此程序允许您键入键盘上的任何字母或符号,并尝试捕获它并计算符号。然后,返回它。

这是我在C

中的代码
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#define N 25
typedef char string[N];

int main(int argc, char *argv[]) 
{
    int i,j;
    int jumlah[10];

    string inputan;
    string temp;
    int counter;

    //Init
    for(i=0;i<10;i++) {
        jumlah[i]=0;
    }

    for(i=0;i<10;i++) {
        temp[i]='-';
    }

    for(i=0;i<10;i++) {
        inputan[i]='-';
    }


    do {

        system("cls");
        printf("\nMasukan kalimat: ");fflush(stdin);gets(inputan);

        if(strcmpi(inputan,"0")!=0) {
            strcpy(temp,inputan);
        }
        getch();

    }while(strcmpi(inputan,"0")!=0);


    printf("Hasil Analisa:\n\n");

    for(i=0;i<10;i++) {

        if(temp[i]!='-') {

            char c = temp[i];
            for(j=0;j<10;j++) {

                if(temp[j]!='-') {

                    if(c == temp[j])
                        counter+=1;

                }

            }

            jumlah[i] = counter;
            counter = 0;
        }

    }


    for(i=0;i<10;i++) {

        if(temp[i]!=' ' && temp[i]!='-' && temp) {
            printf("\t%c terdapat %d\n",temp[i],jumlah[i]); 
        }

    }

    getch();

}

这是我的控制台结果:

There's a space

这样,程序将显示空格符号并对其进行计数。

如果我可以再次询问,如果在另一个具有相同符号的索引中再次出现符号,如何只显示一个字符。谢谢,如果我的英语不流利,请原谅我。

1 个答案:

答案 0 :(得分:0)

打印输出结束时显示的空格是因为您包含的测试条件列表:

if(temp[i]!=' ' && temp[i]!='-' && temp)

可能缺少一些需要排除的附加条件:
1)增加了额外的测试:test[i] != 0
2)将temp[i] != ' '更改为!isspace(temp[i]),这将测试所有空格。

添加完成后:

if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0))

输入的文本仅打印到最后一个非空白字符。

代码修改:
我在下面的代码中添加了一些其他的小修改,允许在我的环境中编译代码。因为我的修改使用属于C标准库的函数,所以这也应该为您编译。 更改还包括扩展for(...)循环以适应您创建的数组大小,允许输入最多N-1个字符而不是仅10个。我所做的大部分内容包括注释解释。

int main(int argc, char *argv[]) 
{
    int i,j;
    //int jumlah[10]; 
    int jumlah[N]; // probably meant to use N here?

    string inputan = {0};
    string temp = {0};
    int counter = 0;// initialize

    for(i=0;i<N;i++) {
        jumlah[i]=0;
    }

    for(i=0;i<N-1;i++) {
        temp[i]='-';
    }

    for(i=0;i<N-1;i++) {
        inputan[i]='-';
    }


    do {

        //system("cls");  This is fine, just does not work in my environment, so commented.
        //printf("\nMasukan kalimat: ");fflush(stdin);gets(inputan);
        printf("\nPut Sentence (or \"0\" to process): ");fflush(stdin);gets(inputan);// clarified instructions.

        if(stricmp(inputan,"0")!=0) {  //strcmpi
            strcpy(temp,inputan);
        }
        //getch(); this (or getchar()) is really not necessary here to support
        //         the flow of your application.

    }while(stricmp(inputan,"0")!=0);



    printf("Hasil Analisa:\n\n");

    for(i=0;i<N;i++) { //replace 10 with N

        if(temp[i]!='-') {

            char c = temp[i];
            for(j=0;j<N;j++) {  //replace 10 with N

                if(temp[j]!='-') {

                    if(c == temp[j])
                        //counter+=1;
                        counter++; // var++ equivalent var += 1

                }

            }

            jumlah[i] = counter;
            counter = 0;
        }

    }


    for(i=0;i<N;i++) {

        //if(temp[i]!=' ' && temp[i]!='-' && temp) { // only spaces ?
        if(!isspace(temp[i]) && temp[i]!='-' && temp && (temp[i] != 0)) { // temp[i] != 0, and exclude all white space
            printf("\t%c terdapat %d\n",temp[i],jumlah[i]); 
        }

    }

    getchar();    //orig getch() not standard

}

解决你的问题:如果在另一个具有相同符号的索引中再次出现符号,如何只显示一个字符。

显示所使用字符的列表,以及在单独的函数中可以更好地处理使用的次数。通过插入以下行,可以调整下面的一个在原始主函数中调用:

char *res = letterCounter("this is the string");
printf(res);
free(res);

在您现有的行下:printf("Hasil Analisa:\n\n"); (即将该行下的所有代码替换为getch();函数;

char * letterCounter(const char *string)
{
    int i, j;
    int len = strlen(string);
    char *dup = StrDup(string);
    if(!dup) return NULL;
    int viewableAscii = '~' - '!'; /// range of ASCII from ! to ~ (33 - 126)
    char buf[20];
    char * results = calloc(100*strlen(string), 1);//ensure enough room
    if(!results) return NULL;
    /// caps 'A' == 65, 'Z' == 90
    /// lowr 'a' == 97, 'z' == 122
    /// all visable printables: 33 - 126
    unsigned char characterUsageCounter[viewableAscii];
    memset(characterUsageCounter, 0,viewableAscii); 

    for(i=0;i<len;i++)
    {
        for(j=0;j<viewableAscii;j++)
        {
            if(dup[i]  == 33 + j)
            {
                characterUsageCounter[j]++;
            }
        }
    }
    for(i=0;i<viewableAscii;i++)
    {
        if(characterUsageCounter[i] > 0)
        {
            if(characterUsageCounter[i] == 1) sprintf(buf, "%c occurs %d time\n", i+33, characterUsageCounter[i]);
            else sprintf(buf, "%c occurs %d times\n", i+33, characterUsageCounter[i]);
            strcat(results, buf);
        }
    }

    return results;
}

例如,如果字符串“this is the string”作为参数传递给该函数,则输出以下内容:
enter image description here

相关问题