一个人总结的strcpy等之类的字符串操作
http://blog.csdn.net/cai0538/article/details/6863856
0 最长的公共子串
实现该匹配的函数源代码:
//该函数实现匹配source1与source2中最长公共部分,并将结果存储与dest中
void find_max_str(char *source1,char *source2,char *dest)
//该函数实现将source2中从start到end处所组成的子串与source1匹配若匹配成功则返回1否则返回0
int substring(char *source1,char *source2,int start,int end)
int substring(char *source1,char *source2,int start,int end)
{//将source2的start位置到end位置所组成的串与source1匹配,查找在
//source1中是否存在所组成的串
int i,len1=strlen(source1),j;
for(i=0,j=start;i<len1&&j<=end;)
{
if(source1[i]==source2[j])
{//匹配移动位置
i++;
j++;
}
else
{//不匹配调整位置
i=i-(j-start)+1;
j=start;
}
}
if(j>end)//
return 1;
return 0;
}
void find_max_str(char *source1,char *source2,char *dest)
{//匹配两字符串中的最大子串
char *max,*min;//max表示较长字符串,min表示较短字符串
int len1,len2,i,j,k;
if(source1==NULL||source2==NULL||dest==NULL)
return;
//分别计算两字符串长度
len1=strlen(source1);
len2=strlen(source2);
//找到最长字符串,与最短字符串,并将最短字符串的长度赋值给len2
max=(len1>len2)?source1:source2;
min=(max==source1)?source2:source1;
len2=(len1<len2)?len1:len2;
//max min分别为source1与source2中较长字符串和较短字符串
for(i=0;i<len2;i++)
{
for(j=0,k=len2-i-1;k<len2;k++,j++)//j代表图中的start位置,k代表图中的end位置
if(substring(max,min,j,k))
//从较短字符串的j到k位置去匹配较长字符串,查看较长字符串中是否包含较短字符串的j到k的所有字符
{
strcpy(dest,min+j);//找到则将字符该匹配字符串赋值给目标结果
*(dest+k-j+1)='\0';
return;
}
}
}
1 统计英文文本句子数码,文本只包括大小写英文字母,空格,点号,逗号,完整句子至少有一个字母,以点号结束。
public static int sentensNum() throws IOException
{
int num=0;
File file=new File("e:/english.txt");
BufferedReader red=new BufferedReader(new FileReader(file));
StringBuffer sf=new StringBuffer();
String line=null;
while((line=red.readLine())!=null)
{
//line=red.readLine().toString();
sf.append(line);
}
String[] a=sf.toString().split("\\.");
Pattern exp=Pattern.compile("[a-zA-Z]+");
int n=0;
for(int i=0;i<a.length;i++)
{
Matcher matcher=exp.matcher(a[i].toString());
if(matcher.find()){n++;} }
System.out.println(n);
return n;
}
2 统计文章的单词数据,并且统计每个单词出现的次数
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class UserTreeMap{
public static void main(String args[]) throws IOException{
BufferedReader buf=new BufferedReader(new FileReader("english.txt"));
System.out.println("Read under this dir English.txt");
StringBuffer sbuf=new StringBuffer();
String line=null;
while((line=buf.readLine())!=null){
sbuf.append(line);
}
buf.close();
Pattern expression=Pattern.compile("[a-zA-Z]+");
String string1=sbuf.toString().toLowerCase();
Matcher matcher=expression.matcher(string1);
TreeMap myTreeMap=new TreeMap();
int n=0;
Object word=null;
Object num=null;
while(matcher.find()){
word=matcher.group();
n++;
if(myTreeMap.containsKey(word)){
num=myTreeMap.get(word);
Integer count=(Integer)num;
myTreeMap.put(word,new Integer(count.intValue()+1));
}
else
{
myTreeMap.put(word,new Integer(1));
}
}
System.out.println("统计分析如下:");
System.out.println(""t 文章中单词总数为:"+n+"个");
System.out.println("具体信息查看当前目录的Result.txt文件");
BufferedWriter bufw=new BufferedWriter(new FileWriter("result.txt"));
Iterator iter=myTreeMap.keySet().iterator();
Object key=null;
while(iter.hasNext()){
key=iter.next();
bufw.write((String)key+":"+myTreeMap.get(key));
bufw.newLine();
}
bufw.write("english.txt单词总数为"+n+"¸ö");
bufw.newLine();
bufw.write("english.txt不同单词"+myTreeMap.size()+"个");
bufw.close();
}
}
3
输入:N(整数)
输入:数据文件A.txt,不超过6条记录,字符串长度不超过15个字节
文件格式如下:
字符串\t数字\n
说明:
每行为1条记录;字符串中不含有\t。
数字描述的是该字符串的出现概率,小于等于100的整数。
多条记录的出现概率之和为100,如果A.txt不满足该条件,程序则退出;
如果文件格式错误,程序也退出。
要求:
编写一个程序,输入为N(正整数),读入文件A.txt,按照字符串出现概率随机地输出字符串,输出N条记录
例如:
输入文件A.txt
abc\t20
a\t30
de\t50
输入为:10
即 abc有20%的概率输出,a有30%的概率输出,de有50%的概率输出,输出10条记
录
以下为一次输出的结果,多次输出的结果可能不相同。
abc
a
de
de
abc
de
a
de
a
de
public static void randGailv() throws IOException
{
Scanner reader=new Scanner(System.in);
System.out.print("输入n:");
int x=reader.nextInt();
File file=new File("e:/test.txt");
BufferedReader red=new BufferedReader(new FileReader(file));
//StringBuffer sf=new StringBuffer();
//HashMap map=new HashMap();
//有人用veator,里面用泛型,放个node(string,rate,count//输入n时,这个点应该出现多少次)
List<List> map =new ArrayList<List>();
String line=null;
int flag=0;
while((line=red.readLine())!=null)
{
String[] a=line.split("\\t");
flag+=new Integer(a[1]);
int count=new Integer(a[1])*n/100;
//map.put(a[0], count);
List b=new ArrayList();
b.add(a[0]);
b.add(a[1]);
b.add(count);
map.add(b);
}
if(flag!=100){return ;}
Random rd=new Random();
while (!map.isEmpty()) {
int i = map.size();
int index = rd.nextInt(i);
String item = map.get(index).get(0).toString();
int count = Integer.parseInt( map.get(index).get(2).toString());
System.out.println(item);
count--;
//更新list中count的值,这里有点问题,没写好
if (count == 0)
map.remove(index);
}
}
5 void dele(char *str,char ch),把str中的ch删掉
void del(char * s,char b){
int p=0,q=0;
while(s[p]){
if(s[p]!=b)
s[q++]=s[p];
p++;
}
s[q]=0;
}
6 题目要求把字符串S中所有A字串换成字串B
/*****
*jimmy or kenthy i do not knwon!!!!
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_next(char* t,int next[])
{
int i = 0;
int k = -1;
int len = strlen(t);
next[0] = k;
while(i<len-1)
{
if( k==-1 || t[i]==t[k] )
{
k++;
i++;
if(t[i] != t[k])
next[i] = k;
else
next[i] = next[k];
}
else
k = next[k];
}
}
int kmp_find(char* s,char* t)
{
int i = 0;
int j = 0;
int len1 = strlen(s);
int len2 = strlen(t);
int next[len2];
get_next(t,next);
while(i<len1 && j<len2)
{
if( j==-1 || s[i] == t[j])
{
i++;
j++;
}
else
j = next[j];
}
if(j>=len2)
return i-len2;
else
return -1;
}
char* substr(char* s,char* a,char* b)
{
int len = strlen(a);
int index = kmp_find(s,a);
//kmp find where is a in s or you can use strstr
char* head = s;
*(head+index) = '\0';
char* tail = s + index + len;
//把字符串s分为 head a tail三部分
sprintf(s,"%s%s%s",head,b,tail);
if(kmp_find(s,a) != -1)//如果替换一个后还含有a继续
return substr(s, a, b);
else
return s;
}
int main(int argc, char *argv[])
{
char* s = (char*)malloc(100);
memset(s,0,100);
sprintf(s,"%s",argv[1]);
printf("str:%s\n",s);
printf("sub:%s by %s\nafter:%s\n",argv[2],argv[3],substr(s, argv[2], argv[3]));
free(s);
s=NULL;
system("PAUSE");
return 0;
}
赠送shell实现
echo "xxxxxx" | sed 's/xxx/xxx/g'
awk -v str="sadsadas" 'BEGIN{gsub(/xxx/,"xxxxx",str);print str}'
vi ed等edit内直接s/xx/xxx/g
7 设计函数,输入为一个字符串,里边包含中文、英文、数字等字符,编码为GBK。中文字符的编码规则假定为:双字节组成,高字节大于0x80,低字节任意。
a) 用常用语言(c/c++/php/java)编写函数,实现功能为:按顺序提取输入文本中的中文字符,形成新的文本串返回调用者。
b) 如果调用者有时希望得到输入串的全中文内容,有时希望得到英文内容,那么该函数应如何设计。
c) 如果调用者希望获取输入串中包含中文、英文、数字这三种字符中的一种或多种的需求不定时,函数应如何设计。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class subString {
//截取中文字符
public static String getChinese(String str)throws UnsupportedEncodingException{
String subChinese="";
for(int i=0;i<str.length() ;i++){
char ch=str.charAt( i);
byte [] buf =(ch+"").getBytes("GBK");
if (buf.length >1){
subChinese+=ch;
}
}
return subChinese;
}
//截取英文字符
public static String getEnglish(String str) throws UnsupportedEncodingException{
String subEnglish="";
for (int i=0;i<str.length() ;i++){
char ch=str.charAt( i);
byte[] buf=(ch+"").getBytes("GBK");
if (buf.length ==1 && Character.isLetter( ch)){
subEnglish+=ch;
}
}
return subEnglish;
}
//截取数字
public static String getDigit(String str) throws UnsupportedEncodingException{
String subDigit="";
for(int i=0;i<str.length() ;i++){
char ch=str.charAt( i);
byte[] buf=(ch+"").getBytes("GBK");
if (buf.length ==1 && Character.isDigit(ch)){ subDigit+=ch;
}
}
return subDigit;
}
public static void main(String[] args) throws UnsupportedEncodingException {
final int CHINESE=0x1;
final int ENGLISH=0x2;
final int DIGIT=0x3;
int type;
String str = null;
String substr=null;
try {
str = new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println("源字符串为:"+str);
System.out.println("请输入要截取字符串的类型:1.中文 2.英文 3.数字");
Scanner sc=new Scanner(System.in);
type=(int)sc.nextInt();
if (type==1){
substr=getChinese(str);
System.out.println(" 截取中文字符为:");
System.out.println(substr);
}
else if (type==2){
System.out.println("截取英文字符为:");
substr=getEnglish(str);
System.out.print(substr);
}
else if (type==3){
System.out.println("截取数字为:");
substr=getDigit(str);
System.out.print(substr);
}
}
}