shell脚本 | C/C++ 统计文件单词个数

简介: shell脚本 | C/C++ 统计文件单词个数

1、用shell 脚本获取当前环境的ip地址?

ifconfig | grep "inet addr" | grep "192.168.220"| awk '{print $2}' | tr -d "addr:"

2、简单统计单词的个数

#include <stdio.h>
#define IN    0
#define OUT   1
#define INIT  OUT
int countword(char * filename)
{
  int status = INIT;
  int res = 0;
  if(filename == NULL)  return -1;
  FILE * fp = fopen(filename,"r");
  if(fp == NULL)  return -1;
  char ch;
  while((ch  = fgetc(fp)) != EOF)
  {
    if('\n' == ch || '\t' == ch || ' ' == ch || ',' == ch || '.' == ch || '!' == ch || '\'' == ch || '\"' == ch || '\r' == ch)
    {
      status = OUT;
    }
    else if(status == OUT)
    {
      status = IN;
      res++;
    }
  }
  return res;
}
int main(int argc,char *argv[])
{
  if(argc < 2)  return -1;
  printf("the word is %d\n",countword(argv[1]));
  return 0;
}

拓展

1、统计一个文件中的单词数量,每个单词的数量分别是多少?

#include <iostream>
#include <map>
#include <fstream>
#include <map>
using namespace std;
#define INFO(msg) {cout<<msg<<endl;}
int main(int argc,char *argv[])
{
  if(argc < 2)
    INFO("you need 2 arguments");
  fstream ff(argv[1],ios::in);
  if(!ff.is_open()) INFO("open fail");
  string data;
  map<string,int> m;
  while(ff>>data)
  {
    if(m.find(data) == m.end()) 
      m.insert(make_pair(data,1));
    else
      m.find(data)->second++;
  }
  for(map<string,int>::iterator it = m.begin();it != m.end();it++)
    if(it->first.size() < 8)
      cout<<it->first<<"\t: \t"<<it->second<<endl;
    else
      cout<<it->first<<": \t"<<it->second<<endl;
  return 0;
}

2、linux中如何解决scanf数组溢出的问题?

 

 

相关文章
|
1天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形3
【4月更文挑战第29天】
4 0
|
1天前
|
存储 弹性计算 运维
使用shell 脚本打印图形2
【4月更文挑战第29天】
5 0
|
1天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形1
【4月更文挑战第29天】
5 0
|
1天前
|
存储 弹性计算 运维
调整虚拟机内存参数的shell 脚本
【4月更文挑战第29天】
6 0
|
1天前
|
弹性计算 运维 Shell
从shell脚本发送邮件
【4月更文挑战第29天】
9 0
|
1天前
|
弹性计算 运维 Shell
使用 shell 脚本打印图形
【4月更文挑战第29天】
7 1
|
1天前
|
存储 弹性计算 运维
调整虚拟机内存参数的 shell 脚本
【4月更文挑战第29天】
10 2
|
2天前
|
关系型数据库 MySQL Shell
备份 MySQL 的 shell 脚本(mysqldump版本)
【4月更文挑战第28天】
7 0
|
2天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(82)
【4月更文挑战第28天】shell脚本解析及训练(82)
6 1
|
2天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(68)
【4月更文挑战第28天】shell脚本解析及训练(68)
6 0