English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

如果輸入一個句子 如you are my teacher
要用指令找出裡面的e有幾個a有幾個字元
跟計算怎句的長度該怎麼做呢?
計算長度我已經寫出來了 只是如何尋找字元的指令不知道該用什麼下去寫

2007-03-19 13:20:04 · 2 個解答 · 發問者 sauce 1 in 電腦與網際網路 程式設計

2 個解答

//Power by Visual Studio 2005

#include

#include

#include

int main(int argc, char* argv[]){

//==========START==========//

int i=0;

char ch='e',*pCH,str[]="you are my teacher";

pCH=strchr(str,ch);

do{

pCH++;

i++;

}while((pCH=strchr(pCH,ch))!=NULL);

printf("Found %d %c characters.\\n",i,ch);

//==========END==========//

system("PAUSE");

return 0;

}

2007-03-19 14:20:50 · answer #1 · answered by Big_John-tw 7 · 0 0

#include
#include
#include
int GetCharCount(char* sentence , char c );
void main()
{
char* str ="you are my teacher";
char c = 'a';
printf("字串%s有字元%c共%d個\n",str,c,GetCharCount(str,c));
system("PAUSE");
}
int GetCharCount(char* sentence , char c )
{
int Count = 0 ;
while ( sentence[0]!= 0 )
{
if ( sentence[0]== c )
Count ++;
sentence++ ;
}
return Count ;
}

2007-03-19 14:03:28 · answer #2 · answered by 3 · 0 0

fedest.com, questions and answers