Find a Word in a String Without Find Function

Split from Necro'd topic

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 16375 Views - Last Post: 03 December 2010 - 01:39 PM Rate Topic: -----

#16 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 01 December 2010 - 02:23 PM

wow. that article about gets() was insightful, but thing is i haven't yet studied fgets or stdin but i looked it up online and it's basically the same concept as gets(), only that its said to be more efficient.

the program i am making is of a find function except you can't include the string library. i have to print the no of occurrences of the word i am finding.
i finally spotted my mistake in the above program,and got it to work, but its just making another little error.
as for the mistake in the above program, by placing cout checks in every loop i realized i wasn't even entering the first for loop. because and int type variable doesn't have a '\0'.
and i had to increment i in one of the loops i used in between.
and i had a logic, but after a gruesome night of trying to making it work, i just deleted it and tried with a different logic which i obviously first implemented on an example on paper.

as for whether i am studying c or c++, i am studying c++. this is my first ever semester of intro to programming.
thanks for the indentation tip, i shall try and keep it in mind. :)
Was This Post Helpful? 0

#17 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Find a Word in a String Without Find Function

Posted 02 December 2010 - 05:29 AM

Well done on sticking with it and getting to an answer (even if it has a small bug).

Working through these early challenges will make the ones coming easier because we all make mistakes and the real challenge is how to find them and correct them.

Hope things keep going well for you in your course and with your good attitude to problem solving I am confident they will.
Was This Post Helpful? 0
  • +
  • -

#18 budfox  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-December 10

Re: Find a Word in a String Without Find Function

Posted 03 December 2010 - 01:39 PM

Without any string-function from lib and work also if s2 is a substring from a word in s (in this case, no num increment!):
  #define MAXSTR 50
int main()
{
  char s[MAXSTR],s2[MAXSTR];
  int c,i,i1,i2,num=0;

  printf("Enter any string : "); c=0;
  for(i=0;i<MAXSTR-1 && c!='\n';i++)
  {
    c=getchar();
    s[i]=c;
  }
  if( c=='\n') i--;
	s[i]='\0'; i1=i;


  printf("Enter any string : "); c=0;
  for(i=0;i<MAXSTR-1 && c!='\n';i++)
  {
    c=getchar();
    s2[i]=c;
  }
  if( c=='\n') i--;
	s2[i]='\0'; i2=i;

  for(i=0;i<i1;++i)
  {
    c=0;
    if( i==0 || s[i-1]==' ' )
      for(;c<i2;++c)
        if( s[i+c]=='\0' || s2[c]!=s[i+c] ) break;
    if( s2[c]=='\0' && (s[i+c]=='\0' || s[i+c]==' ') ) ++num;
  }

  printf("\n\noccurences are %d",num);
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2