ParsX.com
پذیرش پروژه از دانشجویی ... تا سازمانی 09376225339
 
   ProfileProfile   Log in to check your private messagesLog in to check your private messages  |  FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups Log inLog in   RegisterRegister 

تابع strstr و كد ان source code for strstr function

 
Post new topic   Reply to topic    ParsX.com Forum Index -> C/C++ Programming
View previous topic :: View next topic  
Author Message
vahid
بي تو هرگز


Joined: 26 Nov 2004
Posts: 3067
Location: Tehran

PostPosted: Sun Jan 16, 2005 8:25 pm    Post subject: تابع strstr و كد ان source code for strstr function Reply with quote

تابع strstr از توابع رشته اي است كه اعلان آن بصورت زير است :
char *strstr(const char *str1,const char *str2)

اين تابع را ميتوانيد در سرفايل string.h ‌ با استفاده از اعلان زير اضافه كنيد تا از ان استفاده كنيد :
#include <stdio.h>

وظيفه تابع ان است كه دو رشته را كه بعنوان آرگومان ورودي برنامه نويس c به ان ميدهد را گرفته و اگر str2 ‌ را در str1 پيدا كرد ادرس اولين كاراكتر مشابهي كه از str2 در str1 پيدا ميكند را در خروجي دهد. وگرنه مقدار null را برگرداند .
كه البته تابعي كه در زير بازسازي شده است مقدار null ‌بر نميگرداند . از دوستان خواهش ميكنم در صورتي كه خطايي در برنامه زير پيدا كرديد حتما براي بهينه شدن ان اطلاع دهند .

/* Developed BY Vahid Aghamohammadi in Order By www.ParsX.com */
/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   report any upgrade to status@gmail.com or parsx.com website TNQ.
 *
 ***************************************************************************/
#include <stdio.h>
char *StrStr(char *,char *);
main(){ char st1[50],st2[50]; /* use malloc for dynamic*/
   char *p;
   int n;
   char *m;
gets(st1);
gets(st2);
p=StrStr(st1,st2);
printf("%p",p);
getche();
return 0;
}
char *StrStr(char *st1,char *st2){
int count=0,n1=0,n2=0,i,j;
int sentinel; /* my boolean-dogPolice */
char *m;
while(*st1){    /* strlen(st1) */
        *(st1++);
        count++;
        }
n1=count;
for(i=count;i>0;i--,*(st1--)); /* back to first */
count=0;
while(*st2){    /*strlen(st1)*/
        *(st2++);
        count++;
        }
n2=count;
for(i=count;i>0;i--,*(st2--));
for(i=0;i<=n1;i++){
   for(j=0;j<=n2;j++){
      if(*(st1+i+j)==*(st1+j))
      {       sentinel=1;
         m=&st1[i];  }
      else {
         sentinel=0;
         break;    }}
   if(sentinel==1) break; }
   return(m);      }


Last edited by vahid on Mon Jan 17, 2005 6:05 pm; edited 1 time in total
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Mon Jan 17, 2005 8:14 am    Post subject: Reply with quote

برای من حتی کامپایل هم نشد Error می ده
Back to top
vahid
بي تو هرگز


Joined: 26 Nov 2004
Posts: 3067
Location: Tehran

PostPosted: Mon Jan 17, 2005 8:38 am    Post subject: Reply with quote

اين برنامه تحت C استاندارد نوشته شده است !
Back to top
arash
مدير بخش سي
مدير بخش سي


Joined: 27 Nov 2004
Posts: 1232
Location: www.parsx.com

PostPosted: Mon Jan 17, 2005 9:12 am    Post subject: Reply with quote

بابا استاندارد !!!
Back to top
vahid
بي تو هرگز


Joined: 26 Nov 2004
Posts: 3067
Location: Tehran

PostPosted: Mon Jan 17, 2005 11:08 am    Post subject: Reply with quote

ديگه ديگه ...
Back to top
lkjk123456
مهمون يكي دو روزه


Joined: 16 Aug 2005
Posts: 8

PostPosted: Wed Aug 17, 2005 11:43 pm    Post subject: Reply with quote

وحید جان تابعgetch()نداریم(یهeزیاد گذاشتی)
همچنینن یه include کم گذاشتی
در ضمن جواب نهایی یه همچین چیزی بود
0012ff58
Back to top
vahid
بي تو هرگز


Joined: 26 Nov 2004
Posts: 3067
Location: Tehran

PostPosted: Thu Aug 18, 2005 6:39 am    Post subject: Reply with quote

چي رو include نكردم ؟
منظورت اينه كه تابع
getche()
نداريم ؟
Back to top
lkjk123456
مهمون يكي دو روزه


Joined: 16 Aug 2005
Posts: 8

PostPosted: Tue Aug 23, 2005 5:56 am    Post subject: Reply with quote

كل منظورم اينه كه ادرس اولين كاراكتر مشابهي كه از str2 در str1 پيدا ميكند را در خروجي چنين چيزي نمايش داده ميشود
0012ff58
لطفا بگو چرا؟
Back to top
Display posts from previous:   
Post new topic   Reply to topic    ParsX.com Forum Index -> C/C++ Programming All times are GMT + 3.5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum