题目:
把给定的字符串以单词的方式反序,其中,给定的子串不倒转,
比如:s[] = "The quick brown fox jumps over the lazy dog";
子串: sub[] = "brown fox";
结果应该是:dog lazy the over jumps brown fox quick The.
Code:
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#define MAX 7
6int CountOfWords(char *buf)//统计一个字串中有多少个单词
7{
8 int nCouter=0;
9 int flag=0;
10 while(*buf!='\0')
11 {
12 if(*buf!=' ')
13 {
14 flag=1;
15 }
16 if(flag==1&&*buf==' ')
17 {
18 nCouter++;
19 flag=0;
20 }
21 ++buf;
22 }
23 if(*(buf-1)!=' ')nCouter++;
24 return nCouter;
25}
26int MaxlenOfString(char *strbuf)
27{
28 int nMax=0,flag=1,counter=0;
29 while (*strbuf!='\0')
30 {
31 if(*strbuf==' '||*strbuf==','||*strbuf=='.'||*strbuf=='-')
32 counter=0;
33 else
34 ++counter;
35 if(counter>=nMax)
36 nMax=counter;
37 ++strbuf;
38 }
39 return nMax;
40}
41
42int CopyString(char *pstart,char *pend,char *buf)
43{
44 while(*pstart!=*pend)
45 {
46 *(buf++)=*(pstart++);
47 }
48 return 0;
49}
50/**//***************************************************/
51int FindSub(char *strbuf,char *sub)//返回要查询的单词在所在句中的第几个单词。
52{
53 char *pstr=NULL,*psub=NULL,*pdes=strbuf,*pbuf;
54 while (*pdes!='\0')
55 {
56 if(*pdes==*sub)
57 {
58 pstr=pdes;
59 psub=sub;
60 while(*psub!='\0')
61 {
62 if(*psub!=*pstr)
63 {
64 break;
65 }
66 ++psub;
67 ++pstr;
68 }
69 if(*psub=='\0')
70 {
71 pbuf=(char *)malloc(sizeof(char)*(pdes-strbuf+1));
72 CopyString(strbuf,pdes,pbuf);
73 strcat(pbuf,"\0");
74 return CountOfWords(pbuf);//返回单词个数位置
75 //return pdes-strbuf;返回字母个数的位置
76 }
77 }
78 ++pdes;
79 }
80 return 0;
81}
82
83int GetWord(char *strbuf,char *outbuf,int num)
84{
85 int i=1,flag=0,ncounter=0;
86 if(num>CountOfWords(strbuf))return 0;
87 while(i<num&&*strbuf!='\0')
88 {
89 if(*strbuf!=' ')
90 {
91 flag=1;
92 }
93 if(flag==1&&*strbuf==' ')
94 {
95 ++i;
96 flag=0;
97 }
98 ++strbuf;
99 }
100 if(i>=num)
101 {
102 while(*strbuf!=' ')
103 {
104 *(outbuf++)=*(strbuf++);
105 }
106 return 1;
107 }
108 return 0;
109}
110
111int DiveString(char *strbuf,char oubuf[][MAX])
112{
113 int i=0;
114 int wordssum=CountOfWords(strbuf);
115 for(i=1;i<=wordssum;i++)
116 {
117 GetWord(strbuf,oubuf[i-1],i);
118 }
119 return 0;
120}
121
122int Reverse(char buf[][MAX],int nstart,int nend)
123{
124 int i=0;
125 char tembuf[10];
126 memset(tembuf,0,sizeof(tembuf));
127 for(i=0;i<(nend-nstart+1)/2;i++)
128 {
129 strcpy(tembuf,buf[i+nstart-1]);
130 strcpy(buf[i+nstart-1],buf[nend-1-i]);
131 strcpy(buf[nend-1-i],tembuf);
132 }
133 return 0;
134}
135
136int ComString(char *outbuf,char inbuf[][MAX],int num)
137{
138 int i=0;
139 for(i=0;i<num;i++)
140 {
141 strcat(outbuf,inbuf[i]);
142 strcat(outbuf," ");
143 }
144 strcat(outbuf,"\0");
145 return 0;
146}
147
148void Display(char buf[][MAX],int num)
149{
150 int i=0;
151 for(i=0;i<num;i++)
152 {
153 printf("[%s] ",buf[i]);
154 }
155 printf("\n");
156}
157int main()
158{
159 char buf[]="The quick brown fox jumps over the lazy dog";
160 char sub[]="brown fox";
161 char tet[10];
162 char test[9][MAX];
163 int ncounter=0,nmax=0,i=0;
164 ncounter=CountOfWords(buf);
165 nmax=MaxlenOfString(buf);
166 memset(tet,0,sizeof(tet));
167 memset(test,0,sizeof(test));
168 printf("%s = [%d]\n",buf,CountOfWords(buf));
169 ncounter=CountOfWords(buf);
170 printf("%s = [%d]\n",buf,MaxlenOfString(buf));
171
172 printf("分解后\n");
173 DiveString(buf,test);
174 Display(test,9);
175 printf("逆序之后的\n");
176 Reverse(test,1,9);
177 Display(test,9);
178 Reverse(test,6,7);
179 printf("二次逆序之后的\n");
180 Display(test,9);
181 printf("合并后\n");
182
183 ComString(buf,test,9);
184 printf("%s\n",buf);
185 return 0;
186}
结果:
posted on 2009-06-13 16:16
宇驰 阅读(201)
评论(0) 编辑 收藏 引用