我有這個非常簡單的實驗室任務要做,我需要做的就是打印出字符串中的字符兩次,除非是空格。字符指針和空間搜索
由於我似乎無法弄清楚的原因,「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++;
}
}
我感覺這是關於如何增加指針或isspace函數的。
謝謝你的時間。
編輯:將'/ 0'更改爲'\ 0'。覺得沒有看到這一點愚蠢。
空字符爲** '\ 0' **的改變了首發 – Joel 2014-10-12 13:46:33
。我覺得很愚蠢。這並沒有解決它傷心。 – Shaun 2014-10-12 13:51:26
聽你的編譯器,你可能會得到'strncpy','strlen'和'echoString'(所有不兼容)的隱式聲明警告,'gcc -std = c99 -pedantic -Wall'我得到了錯誤的警告所有'printf'調用的格式字符串和多字符常量的警告。 – mafso 2014-10-12 14:00:08