當前位置:編程學習大全網 - 編程軟體 - 編程用指針方法處理,輸入n個字符串存入壹個二維字符數組,找出其中最長的壹個字符串

編程用指針方法處理,輸入n個字符串存入壹個二維字符數組,找出其中最長的壹個字符串

#include <iostream>

using namespace std;

int strLength(char *begin)

{

char *pch = begin;

int nCount = 0; //計字符數的變量

while (*pch != 0)

{ //發現'\0'則結束循環

pch++; //字符指針移向下壹位

nCount++;

}

return nCount;

}

int main()

{

int j, n;

char szArr[128][128];

cout << "輸入字符串個數: " ;

cin >> n;

for (j = 0; j < n; j++)

{

cin >> szArr[j];

}

int nMaxLength = 0;

int nMaxIndex = 0;

for (j = 0; j < n; j++)

{

if (nMaxLength < strLength(szArr[j]))

{

nMaxLength = strLength(szArr[j]);

nMaxIndex = j;

}

}

cout << "最長的字符串為:" << szArr[nMaxIndex] << endl;

cout << '\t' << "其長度為:" << nMaxLength << "個字符" << endl;

system("pause");

return 0;

}

  • 上一篇:美的空調遙控器使用視頻教程
  • 下一篇:貴州鋁業技師學院地址
  • copyright 2024編程學習大全網