c语言中怎么分行显示字符串呢。。。。。 c语言如何对字符串进行位置查询
C\u8bed\u8a00\u94fe\u5f0f\u4e32\u600e\u4e48\u5206\u884c\u8f93\u51fa\u5b57\u7b261\u3001\u94fe\u5f0f\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6709\u6362\u884c\u7b26\uff1f
\u82e5\u6709\uff0c\u7ec4\u5408\u4e32\u65f6\u5c31\u5e94\u8be5\u8003\u8651\u6362\u884c\u95ee\u9898\u3002
2\u3001\u6ca1\u6709\u65f6\uff0c\u9700\u8981\u6839\u636e\u4e32\u957f\u5ea6\u3001\u5f53\u524d\u663e\u793a\u4f4d\u7f6e\uff0c\u4e32\u4e2d\u5c1a\u5f85\u663e\u793a\u7684\u5b57\u7b26\u6570\uff0c\u7edf\u7b79\u8003\u8651\u3002
\u5305\u542b\u6587\u4ef6\uff1astring.h
\u51fd\u6570\u540d: strstr
\u51fd\u6570\u539f\u578b\uff1a
extern char *strstr(char *str1, const char *str2);
\u8bed\u6cd5\uff1a* strstr(str1,str2)
str1: \u88ab\u67e5\u627e\u76ee\u6807\u3000string expression to search.
str2: \u8981\u67e5\u627e\u5bf9\u8c61\u3000The string expression to find.
\u8fd4\u56de\u503c\uff1a\u82e5str2\u662fstr1\u7684\u5b50\u4e32\uff0c\u5219\u8fd4\u56destr2\u5728str1\u7684\u9996\u6b21\u51fa\u73b0\u7684\u5730\u5740\uff1b\u5982\u679cstr2\u4e0d\u662fstr1\u7684\u5b50\u4e32\uff0c\u5219\u8fd4\u56deNULL\u3002
\u4f8b\u5b50\uff1a
char str[]="1234xyz";
char *str1=strstr(str,"34");
cout << str1 << endl;
\u663e\u793a\u7684\u662f: 34xyz
\u51fd\u6570\u5b9e\u73b0
1.Copyright 1990 Software Development Systems, Inc.
char *strstr(const char *s1,const char *s2)
{
\u3000int len2;
\u3000if(!(len2=strlen(s2)))//\u6b64\u79cd\u60c5\u51b5\u4e0bs2\u4e0d\u80fd\u6307\u5411\u7a7a\uff0c\u5426\u5219strlen\u65e0\u6cd5\u6d4b\u51fa\u957f\u5ea6\uff0c\u8fd9\u6761\u8bed\u53e5\u9519\u8bef
\u3000 return(char*)s1;
\u3000for(;*s1;++s1)
\u3000{
\u3000if(*s1==*s2 && strncmp(s1,s2,len2)==0)
\u3000return(char*)s1;
\u3000}
\u3000return NULL;
}
2.Copyright 1986 - 1999 IAR Systems. All rights reserved
char *strstr(constchar*s1,constchar*s2)
{
int n;
if(*s2)
{
while(*s1)
{
for(n=0;*(s1+n)==*(s2+n);n++)
{
if(!*(s2+n+1))
return(char*)s1;
}
s1++;
}
return NULL;
}
else
return (char*)s1;
}
3. GCC-4.8.0
char *strstr(const char*s1,const char*s2)
{
const char*p=s1;
const size_tlen=strlen(s2);
for(;(p=strchr(p,*s2))!=0;p++)
{
if(strncmp(p,s2,len)==0)
return (char*)p;
}
return(0);
}
\u5e94\u7528\u4e3e\u4f8b
// strstr.c
#include
#include
main()
{
char *s="GoldenGlobalView";
char *l="lob";
char *p;
clrscr();
p=strstr(s,l);
if(p)
printf("%s",p);
else
printf("NotFound!");
getchar();
return0;
}
//\u529f\u80fd\uff1a\u4ece\u5b57\u4e32\u201d string1 onexxx string2 oneyyy\u201d\u4e2d\u5bfb\u627e\u201dyyy\u201d
(\u5047\u8bbexxx\u548cyyy\u90fd\u662f\u4e00\u4e2a\u672a\u77e5\u7684\u5b57\u4e32)
char *s=\u201dstring1onexxxstring2oneyyy\u201d;
char *p;
p=strstr(s,\u201dyyy\u201d);
if(p!=NULL)
printf(\u201c%s\u201d,p);
else
printf("notfound\n");
\u8bf4\u660e\uff1a\u5982\u679c\u76f4\u63a5\u5199\u8bed\u53e5p=strstr(s,\u201done\u201d)\uff0c\u627e\u5230\u7684\u662fonexxxstring2oneyyy
char *mystrstr(char*s1,char*s2)
{
if(*s1==0)
{
if(*s2)
return (char*)NULL;
return (char*)s1;
}
while(*s1)
{
int i=0;
while(1)
{
if(s2[i]==0)
return s1;
if(s2[i]!=s1[i])
break;
i++;
}
s1++;
}
return (char*)NULL;
}
while(buf[i]!=“\0”){
putchar(buf[i]);
i++;
if(i>=2){
i=0;
putchar("\n");
}
}
直接用 if(i%2==0) putchar("\n");
int i=0;
while(buf[i])
{
if(i>0 && (i % linewidth)==0)
putchar('\n');
putchar(buf[i]);
i++;
}
绛旓細int main(void){ char str[1000];//瀹氫箟涓涓瀛楃涓鏁扮粍 char strnew[1000];//瀹氫箟涓涓鐢ㄥ瓧绗︿覆鏁扮粍 char m[] = " ";//瀹氫箟绌烘牸鍙橀噺 printf("璇疯緭鍏ヤ竴涓插瓧绗锛");//鏂囧瓧鎻愮ず杈撳叆瀛楃涓 gets(str);//杈撳叆瀛楃涓 char *p = strtok(str,m);//鍙杝tr涓巑鐨勬寚閽 printf("%s\n",p); ...
绛旓細鍙互鐢╯trtok鍑芥暟锛屾寜鏍囧織鎶婂瓧绗︿覆鍒嗗紑 include <string.h> include <stdio.h> char string[] = "A string\tof,tokens\nand some more tokens";char seps[] = " ,\t\n"; //鎷嗗垎鐨勬爣蹇椾负 绌烘牸锛岄楀彿,\t,\n char *token;void main( void ){ printf( "%s\n\nTokens:\n", strin...
绛旓細include<stdio.h>#include<string.h>int main(void){char a[1000];char aa[1000];char c[] = " ";printf("璇疯緭鍏ヤ竴涓插瓧绗锛");gets(a);char *p = strtok(a,c);printf("%s\n",p);p = strtok(NULL,c);while(p){printf("%s\n",p);p = strtok(NULL,c); }} ...
绛旓細int i=0;while(buf[i]!=鈥淺0鈥濓級{ putchar(buf[i]);i++;if(i>=2){ i=0;putchar("\n");} }
绛旓細瑕佷粠閿洏杈撳叆鍒楄〃锛屽彲浠ヤ娇鐢╥nput()鍑芥暟缁撳悎鍒楄〃鎺ㄥ寮忋傛偍鍙互鎸夌収浠ヤ笅绀轰緥浠g爜鐨勬牸寮忚繘琛岃緭鍏ワ細涓婅堪浠g爜灏嗕粠閿洏璇诲彇涓涓瀛楃涓锛岃瀛楃涓插寘鍚敤绌烘牸鍒嗛殧鐨勫厓绱犮傜劧鍚庯紝split()鍑芥暟灏嗗瓧绗︿覆鎷嗗垎涓轰竴涓垪琛紝骞朵娇鐢╥nt()鍑芥暟灏嗘瘡涓厓绱犺浆鎹负鏁存暟绫诲瀷銆傛渶缁堬紝灏嗗緱鍒颁竴涓暣鏁板垪琛紝瀛樺偍鍦ㄥ悕涓簃y_list鐨勫彉閲...
绛旓細浠g爜濡備笅锛歩nt main(){ float r,h,s;scanf("%f",&r)锛泂canf("%f",&h);s=2*3.1415926*r*r+2*3.1415926*r*h printf("琛ㄩ潰绉槸%f\n",s);return 0;}
绛旓細1銆乬ets鍑芥暟锛氬師鍨嬶細char * gets(char * buffer)锛涘姛鑳斤細浠巗tdin娴佷腑璇诲彇瀛楃涓锛岀洿鑷宠鍒版崲琛岀鎴朎OF鏃跺仠姝紝骞跺皢璇诲彇鐨勭粨鏋滃瓨鏀惧湪buffer鎸囬拡鎵鎸囧悜鐨勫瓧绗︽暟缁勪腑銆傛崲琛岀涓嶄綔涓鸿鍙栦覆鐨勫唴瀹癸紝璇诲彇鐨勬崲琛岀琚浆鎹负 \0 绌哄瓧绗︼紝骞朵互姝ょ粨鏉熷瓧绗︿覆锛涜鏄庯細褰撲娇鐢╱nicode瀹藉瓧绗︽枃鏈椂锛岃浣跨敤瀹藉瓧绗︾増鏈...
绛旓細鍦C璇█涓锛屾崲琛岀\n閫氬父琚敤鏉ヨ〃绀烘崲琛岋紝鍗冲湪杈撳嚭涓皢鍐呭绉诲埌涓嬩竴琛屻備负浠涔堣鎶奬n鏀惧湪瀛楃涓鐨勬湯灏惧憿锛熻繖鏄洜涓哄湪C璇█涓紝瀛楃涓叉槸浠ョ┖瀛楃\0缁撳熬鐨勫瓧绗︽暟缁勩傝繖涓┖瀛楃琛ㄧず瀛楃涓茬殑缁撴潫銆傚鏋滀綘鍦ㄥ瓧绗︿覆涓彃鍏n锛堟崲琛岀锛夊苟涓斾笉鎶婂畠鏀惧湪瀛楃涓茬殑鏈熬锛岄偅涔堝湪杈撳嚭杩欎釜瀛楃涓叉椂锛屽悗闈㈢殑鍐呭灏嗕細...
绛旓細1銆侀鍏堟垜浠柊寤轰竴涓猟ev C++鐨勯」鐩2銆佹帴涓嬫潵鍦ㄩ」鐩腑鏂板缓C璇█绋嬪簭鏂囦欢銆3銆佺劧鍚庡湪C璇█鏂囦欢涓0鏄庝竴涓瓧鑺傛暟缁勩4銆佹帴涓嬫潵鎴戜滑閫氳繃printf鍑芥暟鎻愮ず鐢ㄦ埛杈撳叆瀛楃涓诧紝閫氳繃scanf鎺ユ敹鐢ㄦ埛杈撳叆鐨勫瓧绗︿覆銆5銆佹帴鐫鍦ㄩ氳繃printf鍑芥暟杈撳嚭鐢ㄦ埛杈撳叆鐨勫唴瀹广6銆佹渶鍚庤繍琛孋璇█绋嬪簭锛屽氨鍙互鐭ラ亾鍐呭锛屾垜浠緭鍏ュ唴瀹逛互鍚庡氨...
绛旓細鍙互璁惧畾鏂囦欢鍚嶄负in.txt, 瀛樻湁涓绯诲垪鏁村瀷鏁版嵁锛屼互绌烘牸鎴栨崲琛屽垎闅斿啓銆備唬鐮佸涓嬶細include int main(){ int v[100];//寮涓涓冻澶熷ぇ鐨勬暟缁勩俰nt i = 0, j;FILE *fp;//鏂囦欢鎸囬拡 fp = fopen("in.txt", "r");//浠ユ枃鏈柟寮忔墦寮鏂囦欢銆俰f(fp == NULL) //鎵撳紑鏂囦欢鍑洪敊銆俽eturn -1;while...