Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 按下键时,如何从字符数组中删除字符?_C - Fatal编程技术网

C 按下键时,如何从字符数组中删除字符?

C 按下键时,如何从字符数组中删除字符?,c,C,这是我的密码: #include <stdio.h> void main(void){ char myString[81]; int x,y,z=0; while(x<81){ system("cls"); printf("Please input a string 80 characters or less. Press space to enter your string. >>>");

这是我的密码:

#include <stdio.h>
void main(void){
    char myString[81];
    int x,y,z=0;
    while(x<81){
        system("cls");
        printf("Please input a string 80 characters or less. Press space to enter your string.  >>>");
        for(y=0;y<x;y++){
                printf("%c",myString[y]);
        }
        myString[x]=getch();
        if ((int)myString[x] == 13){
            myString[x]="\0";
            break;
        }
        if ((int)myString[x] == 8){
            /*Remove Character code here*/
            x=x-1;
        }
        printf("\n");
        x=x+1;
        z=x;    
    }
    printf("\nYou typed \n");
    for(y=0;y<z;y++){
                printf("%c",myString[y]);
    }
}
#包括
真空总管(真空){
char-myString[81];
int x,y,z=0;

而(x我认为您必须使用指针。如果您指向来自要删除的字符的索引,问题将得到解决。请按如下方式定义数组:

char myString[] = "This is string";
char *pMyString = myString;
然后指向下一个索引,而不是要删除的索引

for (char *pMyString  = myString; *pMyString != '\0'; pMyString++)
    *pMyString = *(pMyString+1);
*pMyString = '\0';

我找到了一个简单的解决方案。在你的删除部分中,只要快速浏览一下,将
x=x-1
更改为
x=x-2;
更改为
x=x-2;
是否有帮助?“printf(“请输入一个字符串80个字符”,然后code执行printf:
对于(y=0;y
int x,y,z=0);而(x第一个
while()中的
x
值是多少)
loop?@ceruleanchonii是的,这是因为当字符串为80个字符时,循环会自动停止long@chux我试图使它看起来像一个
scanf
语句。这是为了帮助我理解不同的语句是如何工作的。并且
x
等于零当我使用它时,我得到错误
18 4 E:\stringGet.c[错误]“for”循环初始声明只允许在C99或C11模式下使用。
。这是一个问题。我希望它会:)@RamiroRocha@RamiroRochaAFAIKC11是最新的C标准,那么,早期的或稍后的C编译器如何发出错误消息?稍后的编译器可以是C++ 14。您编译为C++代码吗?