Thread: Substring Help (Simple?)

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Unhappy Substring Help (Simple?)

    Hey guys, i'm pretty new to C as well.

    I read into two strings using getchar()...

    now i'm trying to find out IF the the second string is inside the first string.

    ex:

    string one: Hello, let's go riding.
    string two: let's go

    so, string two WOULD be. Also, I do not want to use strstr()...but something else of some sort.

    Any help is greatly appreciated. Thanks!
    Last edited by heynowletsgo; 10-17-2007 at 12:08 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why don't you want to use strstr(), is that your homework, to write that function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    Why don't you want to use strstr(), is that your homework, to write that function?
    Yeah, that's a very easy way to do it. I need to know another way to do it...that's a little tougher.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I do not want to use strstr()
    Why not?

    >but something else of some sort.
    That would involve you writing the equivalent of strstr. Is that what you want?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by Prelude View Post
    >I do not want to use strstr()
    Why not?

    >but something else of some sort.
    That would involve you writing the equivalent of strstr. Is that what you want?
    the professor wants something other than strstr. i cant figure it for the life of me. yeah, something that is equivalent. i think it needs to be some sort of counter?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    strncmp and a loop ?
    a regex library ?
    Write your own strstr ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    strncmp and a loop ?
    a regex library ?
    Write your own strstr ?
    ha, we haven't even learned strncmp yet.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it looks like you're going to have to come up with something yourself.

    I've gotten bored with playing "20 questions" just to guess what feasible solution might be within your grasp.

    I'm going to assume that you know how to write strcmp(), therefore you should be able to write strncmp()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    			for( i = 0; i < strlen( myString ); i += strlen( myString2 ) )
    			{
    				if( myString[i] != myString2[i] )
    				{
    					same = false;
    					break;
    				}
    			}
    Calculating strlen() is rather expensive, and you might want to store the result of strlen( myString ) into a variable outside of the loop and use that instead.

    Besides, I think you have a buffer overrun of myString2 there . . .

    I'm sure you can think of better names than myString and myString2.

    Code:
    	for( a = 0; a < MaxStringLen; a++ )
    	{
    		
    		myString[a] = getchar();
    		
    		if( myString[a] == '\n' )
    			break;
    	}
    	
    	myString[a] = '\0';
    If the user enters MaxStringLen characters, the last statement writes a character one beyond the end of the array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    I think he/she wants to know the basic layout to loop through string 2 to see if it is a sustring of string 1.
    As i understand it these are ok:

    strlen
    strcopy
    sizeof

    if else
    for loop
    do while

    is ok but no other. ie strstr strcomp etc
    in other words c programming 101

    I can't--who can

    remember bsic layout for it

    thanks

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    any takers?
    Please and thank you

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you seem to have a pretty good grasp of the problem, why not give it a shot.
    Even if you're wrong, you'll at least learn something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    Quote Originally Posted by Salem View Post
    Well you seem to have a pretty good grasp of the problem, why not give it a shot.
    Even if you're wrong, you'll at least learn something.
    WoW!
    someone here seems to be pretty impressed with themselves.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by fireman View Post
    WoW!
    someone here seems to be pretty impressed with themselves.
    You should be careful about the way you talk for other people inside the forum, these people like Salem care for all and try to help, i will give you some code to solve your prob but this is for the last time, learn to work alone and in case of problem ask by showing some code and get your help eventually.

    Code:
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //Find a substring inside a string.
    
    int ManualStrStr(const char *string, const char *substring)
    {
    	if(!string || !substring)
    	{
    		printf("NULL parameter error, check strings.\n");
    		return -1;
    	}
    	else
    	{
    		/* Declare variables. */
    		int i,j = 0;
    		int k = 0;
    		/* Get the string lengths. */
    		int strLen = strlen(string);
    		int substrLen = strlen(substring);
    		if(substrLen > strLen)
    		{
    			printf("Error, Substring has bigger length than the main String.\n");
    			return -2;
    		}
    		else
    		{
    			for(i = 0; i < strLen; i++)
    			{
    				if(substring[j] == string[i])
    				{
    					for(k = i, j = 0; j < substrLen && substring[j] == string[k]; j++,k++);
    					if(j == substrLen)
    					{
    						printf("Substring '%s' was found in String '%s'.\n", substring, string);
    						return 0;
    					}
    					else
    						j = 0;
    				}
    			}
    			/* Exits this for loop means that the string was not found. */
    			if(i == strLen)
    			{
    				printf("Substring '%s' was not found in String '%s'.\n", substring, string);
    				return -3;
    			}
    			else
    			{
    				printf("Some kind of error stopped the loop earlier.\n");
    				return -4;
    			}
    		}
    	}
    }
    
    //---------------------------------------------------------------------------
    int main(int argc, char* argv[])
    {
    	char string[128] = "";
    	char substring[64] = "";
    	printf("Give me the initial string:");
    	fgets(string,sizeof(string),stdin);
    	printf("Give me the potential substring:");
    	fgets(substring,sizeof(substring),stdin);
    	string[strlen(string)-1] = 0;
    	substring[strlen(substring)-1] = 0;
    	ManualStrStr(string, substring);
    	printf("Hit enter to continue....\n");
    	getchar();
    	return 0;
    }
    //---------------------------------------------------------------------------

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > WoW!
    > someone here seems to be pretty impressed with themselves.
    I see a lot of whining in this post, but no actual effort on the part of people wanting to learn.

    Check around the forum, there's plenty of examples where I give lots of help to those people who've made the effort, but for the small minority of spongers like you - you're not getting squat except for an ear-full.

    If you can't summon up the will to even make a bad attempt at this problem (which lets be honest is one of the simplest things to do in programming terms), then you're simply not going to make it as a programmer no matter how many examples you stare at to convince yourself that you might even be able to do this yourself for real.

    Even the best "cordon bleu" chefs probably started out with a few "cordon noir" offerings
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Substring hunt and gather
    By reRanger in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2004, 01:56 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM