C 字符指针和空间搜索

C 字符指针和空间搜索,c,pointers,char,C,Pointers,Char,我有一个非常简单的实验任务要做,我需要做的就是把字符串中的字符打印两次,除非是空格 出于一个我似乎无法理解的原因,“echoString”函数正在无限循环 #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(){ char* rhyme1 = "Hey Diddle diddle the Cat and the fiddle"; char rhyme2[265]; strnc

我有一个非常简单的实验任务要做,我需要做的就是把字符串中的字符打印两次,除非是空格

出于一个我似乎无法理解的原因,“echoString”函数正在无限循环

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

int main(){

char* rhyme1 = "Hey Diddle diddle the Cat and the fiddle";
char rhyme2[265];
strncpy (rhyme2, "The Cow Jumped Over The Moon", sizeof(rhyme2));
char wordList[8][100];

/*Q1: Length of the string rhyme?*/
printf("Length: %d", strlen(rhyme1) );

/*Q2: Print out each letter twice, except for the spaces*/
echoString(rhyme1);

}

void echoString ( char* pString ) {

while ( *pString != '\0' ) {

    if ( !isspace( *pString ) ) {
        printf("%s%s", *pString, *pString);
    }
    else {
        printf("%s", *pString);
    }
    pString++;
}
}
#包括
#包括
#包括
int main(){
char*rhyme1=“嘿,Diddle欺骗了猫和小提琴”;
char-2[265];
strncpy(韵2,“奶牛跳过月亮”,sizeof(韵2));
字符字表[8][100];
/*问题1:字符串押韵的长度*/
printf(“长度:%d”,strlen(韵1));
/*问题2:每个字母打印两次,空格除外*/
回声串(韵1);
}
无效echoString(字符*pString){
而(*pString!='\0'){
如果(!isspace(*pString)){
printf(“%s%s”、*pString、*pString);
}
否则{
printf(“%s”,*pString);
}
pString++;
}
}
我觉得这与我如何递增指针或isspace函数有关

谢谢你抽出时间

编辑:将“/0”更改为“\0”。没有看到这一点,我感到很傻

  • \0
    用于以空结尾的字符,而不是
    /0
    。将
    '/0'
    更改为
    '\0'

  • 使用
    %c
    打印
    字符
    ,而不是
    %s
    ,它代表
    字符串
    。将所有
    %s
    更改为
    %c

  • 错误,因为
    \0
    不是
    /0
    的空字符。因此将其更改为

    while ( *pString != '\0' ) {
    
    那么

    也是错误的,因为您希望每个字符出现两次。请将其更改为

    printf("%c%c", *pString, *pString);
    
    对我来说也是如此

    printf("%s", *pString);
    
    因此,您的程序应该如下所示:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>   //for strlen
    #include <ctype.h>
    
    void echoString(char*); //function prototype
    int main(){
    
    char* rhyme1 = "Hey Diddle diddle the Cat and the fiddle";
    char rhyme2[265];
    strncpy (rhyme2, "The Cow Jumped Over The Moon", sizeof(rhyme2));
    char wordList[8][100];
    
    /*Q1: Length of the string rhyme?*/
    printf("Length: %d\n", strlen(rhyme1) );
    
    /*Q2: Print out each letter twice, except for the spaces*/
    echoString(rhyme1);
    return 0;   
    }
    
    void echoString ( char* pString ) {
    
    while ( *pString != '\0' ) {                 //\0 is NULL not /0
    
        if ( !isspace( *pString ) ) {
            printf("%c%c", *pString, *pString);  //%c for a character
        }
        else {
            printf("%c", *pString);              //%c for a character
        }
        pString++;
    }
    }
    
    #包括
    #包括
    #包括//用于strlen
    #包括
    空回显字符串(字符*)//功能原型
    int main(){
    char*rhyme1=“嘿,Diddle欺骗了猫和小提琴”;
    char-2[265];
    strncpy(韵2,“奶牛跳过月亮”,sizeof(韵2));
    字符字表[8][100];
    /*问题1:字符串押韵的长度*/
    printf(“长度:%d\n”,strlen(韵1));
    /*问题2:每个字母打印两次,空格除外*/
    回声串(韵1);
    返回0;
    }
    无效echoString(字符*pString){
    而(*pString!='\0'){/\0为空,而不是/0
    如果(!isspace(*pString)){
    printf(“%c%c”,*pString,*pString);//%c表示字符
    }
    否则{
    printf(“%c”,*pString);//%c表示字符
    }
    pString++;
    }
    }
    
    空字符首先是“\0”更改了该值。我觉得自己很笨。听你的编译器说,你可能已经收到了关于
    strncpy
    strlen
    echoString
    的隐式声明的警告,使用
    gcc-std=c99-pedantic-Wall
    我会收到所有
    printf
    调用的错误格式字符串的警告和多字符常量的警告。
    printf("%s", *pString);
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>   //for strlen
    #include <ctype.h>
    
    void echoString(char*); //function prototype
    int main(){
    
    char* rhyme1 = "Hey Diddle diddle the Cat and the fiddle";
    char rhyme2[265];
    strncpy (rhyme2, "The Cow Jumped Over The Moon", sizeof(rhyme2));
    char wordList[8][100];
    
    /*Q1: Length of the string rhyme?*/
    printf("Length: %d\n", strlen(rhyme1) );
    
    /*Q2: Print out each letter twice, except for the spaces*/
    echoString(rhyme1);
    return 0;   
    }
    
    void echoString ( char* pString ) {
    
    while ( *pString != '\0' ) {                 //\0 is NULL not /0
    
        if ( !isspace( *pString ) ) {
            printf("%c%c", *pString, *pString);  //%c for a character
        }
        else {
            printf("%c", *pString);              //%c for a character
        }
        pString++;
    }
    }