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数组溢出的问题?