⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p5_42c.cpp

📁 一个文本分析程序。实现分析一段文本中字母
💻 CPP
字号:
// Exercise 5.42 Part C Solution
#include <iostream.h>
#include <string.h>

const int SIZE = 80;

int main()
{
   char text[ 3 ][ SIZE ], *temp, words[ 100 ][ 20 ] = { "" };
   int count[ 100 ] = { 0 }, i;
   
   cout << "Enter three lines of text:\n";
   
   for ( i = 0; i <= 2; ++i )
      cin.getline( &text[ i ][ 0 ], SIZE );
      
   for ( i = 0; i <= 2; ++i ) {
      temp = strtok( &text[ i ][ 0 ], ". \n" );
      
      while ( temp ) {
         int j;

         for ( j = 0; words[ j ][ 0 ] && 
                     strcmp( temp, &words[ j ][ 0 ] ) != 0; ++j )
            ;  // empty body
         
         ++count[ j ];
         
         if ( !words[ j ][ 0 ] )
            strcpy( &words[ j ][ 0 ], temp );
            
         temp = strtok( 0, ". \n" );
      }
   }
   
   cout << '\n';
   
   for ( int k = 0; words[ k ][ 0 ] != '\0' && k <= 99; ++k )
      cout << "\"" << &words[ k ][ 0 ] << "\" appeared " << count[ k ]
           << " time(s)\n";

   cout << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -