C语言(不是C++)中如何从指定字符串中提取指定开始位置到指定结束位置的一个子串给数组? C++如何从String的指定位置搜索一个子串?
c/c++\u4e2d\u600e\u6837\u4ece\u6307\u5b9a\u7684\u4f4d\u7f6e\u62f7\u8d1d\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206C++ :
string str2 = "how are you"\uff1b
string str1=str2.substr(4,3);
C:
char str1[4];
for(int i=0;i<3;++i)
str1[i]=str2[4+i];
str1[3]='\0';
\u53ef\u4ee5\u7528find()\u51fd\u6570\uff0c\u5177\u4f53\u7684\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a
(1) size_type find(E c, size_type pos = npos) const;\u7528\u6765\u67e5\u627e\u5355\u4e2a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u5e76\u8fd4\u56de\u4f4d\u7f6e\u57fa\u4e8e0\u7684\u7d22\u5f15\u503c\u3002
(2) size_type find(const E *s, size_type pos = npos) const;\u7528\u6765\u67e5\u627e\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u5e76\u8fd4\u56de\u8be5\u4f4d\u7f6e\u57fa\u4e8e0\u7d22\u5f15\u503c\u3002
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;\u7528\u6765\u67e5\u627e\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u6570\u7ec4\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u5e76\u8fd4\u56de\u8be5\u4f4d\u7f6e\u57fa\u4e8e0\u7d22\u5f15\u503c\uff0c\u5b83\u662f\u4ecenpos\u5f00\u59cb\u67e5\u627e\u7684\u3002
(4) size_type find(const basic_string &str, size_type pos = npos) const;\u7528\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u5e76\u4e14\u8fd4\u56de\u8be5\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u7684\u57fa\u4e8e0\u7d22\u5f15\u503c\u7684\u4f4d\u7f6e\u503c\uff0c\u5b83\u662f\u4ecenpos\u5f00\u59cb\u67e5\u627e\u7684\u3002
学程序,关键是学方法!
void substr(char *str1,char *str2,int start,int end)
{
int ii;
for (ii=start;ii<=end;ii++)
str1[ii-start]=str2[start-1];
str1[ii-start]='\0';
}
main()
{
char str1[30]="dwew13lkoilesiwokdwok";
char str2[50]="e22dww_dwoiewolksdjfmmm"
char str[50];
substr(str,str1,1,10);
printf("1--%s\n",str);
substr(str,str2,3,18);
printf("2--%s\n",str);
}
1 #include <stdio.h>
2
3 char *sub_str(int start,int end,char *str)
4 {
5 char *sub = NULL;
6 int i = 0;
7 int j = 0;
8 sub = (char *)malloc(end-start);
9 if(NULL == str)
10 return NULL;
11 for(i = start;i<end;i++)
12 {
13 sub[j++] = str[i];
14 }
15
16 sub[end-start]='\0';
17
18 return sub;
19
20 }
21
22 int main ()
23 {
24 char str1[30]="dwew13lkoilesiwokdwok";
25
26 char *sub = NULL;
27
28 sub = sub_str(0,10,str1);
29
30 printf("%s\n",sub);
31
32 return 0;
33 }
绛旓細code 鏄51缂栬瘧鍣ㄨ嚜宸卞鍔犵殑涓涓叧閿瓧锛涓嶆槸鏍囧噯C璇█銆51鍗曠墖鏈哄唴瀛樼粨鏋勬瘮杈冪壒娈婏紝鏈塺om, ram涔嬪垎锛宺am鏈 data,xdata涔嬪垎锛屼笉鍚岀殑鍐呭瓨浣跨敤涓嶅悓鐨勬寚浠よ闂傜幇鍦ㄩ棶棰樻潵浜嗭紝涓涓彉閲忓湪鍐呴儴ram涓紝鍙湁8浣嶅湴鍧锛屾斁鍦▁data涓紝鏈16浣嶅湴鍧锛屾斁鍦╮om涓紝涔熸槸鏈16浣嶅湴鍧锛16浣峳om鍦板潃鏄剧劧涓巟data鍦板潃鐨勫惈涔...
绛旓細1銆佽緭鍏ユ暣鍨嬫暟锛歴canf("%d",&a);寮忎腑%d鏄暣鍨嬫暟鐨勬牸寮忕锛宎鏄敤int a;澹版槑杩囩殑鏁村瀷鏁板彉閲忓悕銆2銆佽緭鍏ユ诞鐐规暟锛歴canf("%f",&b);寮忎腑%f鏄诞鐐规暟鐨勬牸寮忕锛宐鏄敤float b;澹版槑杩囩殑娴偣鏁板彉閲忓悕銆傜劧鍚庯紝绯荤粺灏变細绛夊緟浣犱粠閿洏涓婅緭鍏ヤ竴涓暟瀛椼傝緭鍏ユ暟瀛楀畬姣曟寜鍥炶溅閿粨鏉熻緭鍏ャ備笉鐭ラ亾鏄涓嶆槸瑙e喅浜嗕綘...
绛旓細2銆佹妸A2.c涓畬鏁村畾涔夌殑鍑芥暟鐨勫師鍨嬶紝鍐欏埌涓涓狝2.h鏂囦欢涓紝鐒跺悗鍦ˋ1.c鐨勬枃浠跺ご澧炲姞#include "A2.h"灏卞彲浠ヤ簡銆備互涓婁袱绉嶆柟娉曪紝鍦ˋ1.c涓兘鍙互姝e父鐨勮皟鐢紝灏辫窡鍦ˋ2.c涓皟鐢ㄤ竴鏍枫傚叾瀹炲嚱鏁伴粯璁ゆ槸澶栭儴鐨勶紝鍙鍦ㄥ叾浠栨枃浠朵腑澹版槑灏辫兘浣跨敤锛涗絾鏄敞鎰忓鏋滃湪鍓嶉潰鍔犱笂static锛屽氨鍙兘鍦ㄦ湰鏂囦欢涓娇鐢ㄤ簡锛屼笉鑳...
绛旓細//濡傛灉鎵惧埌鍒欒繑鍥炲湪闀垮瓧绗︿覆涓殑璧峰浣嶇疆, 鑻ユ壘涓嶅埌鍒欒繑鍥-1//瑕佽繑鍥1鎴0, 鏀逛竴涓嬪氨鍙互浜唅nt find(char* source, char* target){int i,j;int s_len=strlen(source);int t_len=strlen(target);if(t_len>s_len){return -1;}for(i=0;i<=s_len-t_len;i++){j=0;int flag=1;...
绛旓細1.鎶婅繖鈥滀竴澶у爢鈥濇暟鏀捐繘涓涓暟缁勩2.鎶婃暟缁勭殑绗竴涓厓绱犵湅浣滄渶澶ф暟璧嬬粰鍙橀噺max銆3.浠庢暟缁勭殑绗簩涓厓绱犲紑濮嬩笌max姣旇緝锛岃嫢鍏冪礌鍊兼瘮max澶у垯鎶婅繖涓厓绱犲艰祴缁檓ax銆4.閬嶅巻鍚巑ax鍒欎负鏈澶х殑銆
绛旓細鐢变簬閫掑綊鍑芥暟涓嶆柇璋冪敤鑷繁鐨勭壒鎬э紝蹇呴』浣跨敤杈圭晫鏉′欢绾︽潫锛屾潵閫鍑洪掑綊锛屽仠姝㈣凯浠o紝鍚﹀垯鏄竴涓笉鏂澶х殑姝诲惊鐜傚湪
绛旓細1銆佺洿鎺ヨ皟鐢ㄧ郴缁熷嚱鏁 system("pause")锛歴ystem()浼氳皟鐢╢ork()浜х敓瀛愯繘绋, 鐢卞瓙杩涚▼鏉ヨ皟鐢/bin/sh-c string 鏉ユ墽琛屽弬鏁皊tring 瀛楃涓叉墍浠h〃鐨勫懡浠, 姝ゅ懡浠ゆ墽琛屽畬鍚庨殢鍗宠繑鍥炲師璋冪敤鐨勮繘绋嬨俿ystem("pause")鍙互瀹炵幇鍐荤粨灞忓箷锛屼究浜庤瀵绋嬪簭鐨勬墽琛岀粨鏋溿傚涓嬶細2銆佽皟鐢╣etch()鍑芥暟锛氭鍑芥暟鏄竴涓笉鍥炴樉鍑芥暟...
绛旓細鍦C璇█涓鏈変互涓嬪叧绯昏繍绠楃锛1) <锛堝皬浜庯級2) <=锛堝皬浜庢垨绛変簬锛3) >锛堝ぇ浜庯級4) >=锛堝ぇ浜庢垨绛変簬锛5) ==锛堢瓑浜庯級6) !=锛堜笉绛変簬锛夊叧绯昏繍绠楃閮芥槸鍙岀洰杩愮畻绗︼紝鍏剁粨鍚堟у潎涓哄乏缁撳悎銆傚叧绯昏繍绠楃鐨勪紭鍏堢骇浣庝簬绠楁湳杩愮畻绗︼紝楂樹簬璧嬪艰繍绠楃銆傚湪鍏釜鍏崇郴杩愮畻绗︿腑锛<銆<=銆>銆>=鐨勪紭鍏堢骇鐩稿悓锛岄珮浜...
绛旓細1]=1...鐩村埌鎵ц瀹宨=9鍚庯紝i鍙樻垚浜10锛屼笉绗﹀悎i<=9鍒欒烦鍑哄惊鐜紝缁х画涓嬮潰鐨勮鍙 for(i=9;i>=0;i--)printf("%d",a[i]);浠巌=9寮濮嬩緷娆″湪灞忓箷涓婅緭鍑篴[9],a[8].a[7]鐩村埌i=0杈撳嚭瀹宎[0]鍚巌=-1 璺冲嚭寰幆鎵ц鏈鍚庝竴鍙 printf("/n");}鍦ㄥ睆骞曚笂鍥炶溅鎹㈣ ...
绛旓細鐢╯scanf纭疄鍙互瀹炵幇锛屼唬鐮佸涓嬶細include int main(){ char str="1.0 2.0 3.0 4.0 50.0";int i ;double d ;i=0;while(str == '')str++ ;while(sscanf(str,"%lf",&d )==1 ){ while(str != '')str++ ;//璺宠繃宸茶杩囩殑鏁 i++;printf("d:%03d= lf\n",i ,d );whil...