Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created December 4, 2010 11:38
Show Gist options
  • Save johnhmj/728122 to your computer and use it in GitHub Desktop.
Save johnhmj/728122 to your computer and use it in GitHub Desktop.
1. 設計一主程式,並呼叫一個副程式StrLen()可以算出字串a[]之字元數目。(不包括 \0)
主程式:(1) 輸入一個字串資料a[]。(2) 呼叫StrLen(),印出字串a[]之字元數目。
副程式:StrLen(),可以算出字串a[]之字元數目,並將結果傳回主程式。
// Integrated Development Environment
// Visual C++
#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 512
unsigned int StrLen(const char* str);
//
int main(int argc, char* argv[])
{
char a[BUFFERSIZE];
printf("Input a string: "), gets(a);
printf("StrLen = %u\n", StrLen(a));
system("PAUSE");
return (0);
}
unsigned int StrLen(const char* str)
{
unsigned int i = 0;
for (; str[i] != '\0'; i ++);
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment