用c需要设计程序截取字符串中第m个开始的n个字符 C语言输入一串字符串从第n个字符开始截取m个字符组成一个新的...
C\u8bed\u8a00\u8f93\u5165\u4e00\u4e32\u5b57\u7b26\u4e32\u4ece\u7b2cn\u4e2a\u5b57\u7b26\u5f00\u59cb\u622a\u53d6m\u4e2a\u5b57\u7b26\u7ec4\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u8f93\u51fa\u30021\u3001\u6211\u4eec\u7f16\u8f91\u5982\u56fe\u4e2d\u8f93\u5165\u7684\u4ee3\u7801\u3002
2\u3001\u8fdb\u5165\u6e90\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b\uff0c\u5728\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4gcc -o t test.cpp\u3002
3\u3001\u8f93\u5165./t\u547d\u4ee4\uff0c\u8fd0\u884c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002
4\u3001\u8fd0\u884c\u7ed3\u679c\u4e3abaidu\u3002
5\u3001strncpy \u7684\u51fd\u6570\u539f\u578bchar *strncpy(char *dest,char *src,size_t n)\uff0c\u6211\u4eec\u5728\u590d\u5236src\u7684\u524dn\u4e2a\u5b57\u7b26\u5230dst\u4e2d\u3002
6\u3001\u5982\u679c\u8d85\u8fc7src\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u81ea\u52a8\u590d\u5236\u5b8c\u5168\u90e8\u7684\u5b57\u7b26\u4e32 \u3002
#include
#include
int main()
{char s[300];
int n,m;
printf("Please enter a string:\n");
gets(s);
printf("n m =?");
scanf("%d%d",&n,&m);
if(n>strlen(s)||m<1)
return 0;
s[n+m-1]='\0';
printf("%s\n",s+n-1);
return 0;
}
方法很多
我用指针写了一种
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
int m,n;
char str[]="abcdffdsfds",*p;
printf("原字符串:%s
",str);
printf("输入m,n值:");
scanf("%d%d",&m,&n);
p=&str[m-1];
printf("从%d个字符开始截取%d个字符:",m,n);
while(n-->0)
printf("%c",*p++);
return 0;
}
void cut_str(char *str, char *dst, int m, int n)
{
int i;
for(i = 0; i < n; i ++)
dst[i]=str[i+m];
dst[n]=0;
}
其实一句话就可以搞定.
strncpy(dst, str+m,n);
绛旓細n; char str[]="abcdffdsfds",*p; printf("鍘瀛楃涓锛%s\n",str); printf("杈撳叆m,n鍊:"); scanf("%d%d",&m,&n); p=&str[m-1]; printf("浠%d涓瓧绗﹀紑濮鎴彇%d涓瓧绗︼細",m,n); while
绛旓細int main(void){ char str1[]="ab678ef0ABCDEcdNijklmnOPQghopqrs345FGHIJKLtuvwxyz12M9RSTUVWXYZ";//瀹氫箟涓涓瘯楠岀敤瀛楃涓 char ch,*p;while(1){ printf("Input a character to find(# end)...\nch=");if(scanf(" %c",&ch),ch=='#')//杈撳叆瑕佹煡鎵剧殑瀛楃,鑻ヤ负#鍒欑粨鏉 break;if...
绛旓細1銆佹垜浠紪杈戝鍥句腑杈撳叆鐨勪唬鐮併2銆佽繘鍏ユ簮鏂囦欢鐨勭洰褰曚笅锛屽湪缁堢杈撳叆鍛戒护gcc -o t test.cpp銆3銆佽緭鍏./t鍛戒护锛岃繍琛岀敓鎴愮殑鍙墽琛屾枃浠躲4銆佽繍琛岀粨鏋滀负baidu銆5銆乻trncpy 鐨勫嚱鏁板師鍨媍har *strncpy(char *dest,char *src,size_t n)锛屾垜浠湪澶嶅埗src鐨勫墠n涓瓧绗﹀埌dst涓6銆佸鏋滆秴杩噑rc鐨瀛楃涓闀垮害锛...
绛旓細1銆侀鍏堟垜浠墦寮缂栫▼杞欢涓荤晫闈紝鐐瑰嚮鐣岄潰涓婃柟鑿滃崟鏍忛噷鐨勬枃浠堕夐」锛屽啀鐐瑰嚮鏂板缓鐩綍涓嬬殑椤圭洰閫夐」銆2銆佺劧鍚庢垜浠夋嫨鎺у埗鍙板簲鐢绋嬪簭閫夐」锛屽啀杩涜鍛藉悕涓瀛楃涓鐨勬埅鍙朣ubstring锛屽啀鐐瑰嚮纭畾鍗冲彲銆3銆佺劧鍚庣郴缁熶細鑷姩鐢熸垚浠g爜銆4銆佸厛鍐欎竴涓瓧绗︿覆鐢ㄤ簬鎴彇娴嬭瘯銆5銆佷娇鐢⊿ubstring()鎴彇our 浠庣11涓瓧绗︿覆寮濮鎴彇鎴...
绛旓細char deq[26]; //璁板綍鍑虹幇瀛楃鐨勯『搴 unsigned int i = 0,j=0;unsigned char letter[26]; //缁熻瀛楃鍑虹幇鐨勬鏁 memset(letter,0,26); //鍒濆瀛楃鍑虹幇娆℃暟涓0 while(*str!='\0'){ letter[*str-'a']++;if(letter[*str-'a']==1)deq[i++]=*str;str++;} for(;j...
绛旓細濡倄='a',瑕佹悶娓呮 瀛楃 鍜 瀛楃涓 鐨勬蹇 鑷充簬浣犺鐨勬彁鍙栧瓧绗︼紝鍙涓嬫柟娉 for(i=0; i<n; i++)//n鏄瓧绗︿覆鍙橀噺y鐨勫瓧绗︿釜鏁 { y[i]=x[i+3];} 杩欐牱灏卞彲浠ヤ簡锛屽氨鏄畝鍗曠殑寰幆璇彞璧嬪硷紝鎶妜[]鏁扮粍绗笁涓瓧绗︹榙鈥欙紙绗0涓瓧绗︽椂'a'锛,浠樼粰鏁扮粍y[0]....
绛旓細flag;//杈撳叆涓涓插瓧绗printf("Input a string:");gets(s);//杈撳叆鎯宠鏌ユ壘鐨勫瓧绗rintf("Input another string:");gets(d);//璋冪敤鍑芥暟锛岃緭鍑虹粨鏋渇lag=SearchString(s,d);if(flag==-1)printf("Not found!\n");elseprintf("Searching results:%d\n",flag);system("pause");...
绛旓細void main(){ char str[256];int i,k,n;gets(str);i=0; k=0; n=1;while ( str[i] ){ if ( str[k]<str[i] ) { k=i; n=1; } else if ( str[k]==str[i] ) n++;i++;} printf("瀛楃鍊兼渶澶у瓧绗︿负%c锛岀1娆″嚭鐜颁綅缃%d锛屽嚭鐜版鏁%d\n",str[k],k,n);} ...
绛旓細include<string.h> intmain(){ charstr1[]="123456",str2[7];inti,j=0,len;len=strlen(str1);for(i=len-1;i>=0;i--){ str2[j++]=str1[i];} str2[6]='\0';printf("%s",str2);return0;} //鏂规硶浜屾寚閽堟硶 include<stdio.h> include<string.h> intmain(){ charstr1[]=...
绛旓細char s[20][16]; // 鍋囧畾 鐢ㄩ楀彿鍒嗛殧 鐨勯儴鍒 绾20涓紝姣忎釜闀垮害 涓嶈秴杩16瀛楃 double d[20]; // 鍋囧畾鏁版嵁涓暟 涓嶈秴杩 20 涓 int i,j=0,L,n=0;fgets(str,200,stdin); // 璇诲叆涓琛 瀛楃涓锛屽惈鎹㈣绗 L = strlen(str); //璁$畻杈撳叆鐨勫瓧绗︿覆闀垮害 for (i=0;i<L;i ...