Thread: strings?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    strings?

    i have this program i have been working on and im at the point wher i dont know what string compare or type of string to use or compose it into my code.
    -----------------------------------------------------------------------------------
    the program should prompt the user to input two character strings(example the imput may say "goodbye" and "bye")...

    the program should have a function that searches the first character string for the second string and return the location of the string....

    in the example if the user enters "goodbye" and "bye" the return value from the program would be 5.
    -----------------------------------------------------------------------------------
    i cant get the 5 and im not using the right string pattern in my code if you could help me out in any way i would appreciate it...


    thanks

  2. #2
    What i think you're looking for is strchr().

    Looking it up in my help files i come across this handy little example:

    Code:
    /* STRCHR.C: This program illustrates searching for a character
     * with strchr (search forward) or strrchr (search backward).
     */
    
    #include <string.h>
    #include <stdio.h>
    
    int  ch = 'r';
    
    char string[] = "The quick brown dog jumps over the lazy fox";
    char fmt1[] =   "         1         2         3         4         5";
    char fmt2[] =   "12345678901234567890123456789012345678901234567890";
    
    void main( void )
    {
       char *pdest;
       int result;
    
       printf( "String to be searched: \n\t\t%s\n", string );
       printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
       printf( "Search char:\t%c\n", ch );
    
       /* Search forward. */
       pdest = strchr( string, ch );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "Result:\tfirst %c found at position %d\n\n", 
                  ch, result );
       else
          printf( "Result:\t%c not found\n" );
    
       /* Search backward. */
       pdest = strrchr( string, ch );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "Result:\tlast %c found at position %d\n\n", ch, result );
       else
          printf( "Result:\t%c not found\n" );
    }
    Enjoy.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    looking at the example it looks like strchr only finds the instance(s) of a character and not a string. i think The WaRped OnE wanted to search a string for a series of characters, like i've done below. but hey, i could be wrong.

    Code:
    #include<iostream.h>
    #include<string.h>
    
    int main()
    {
    	char String_To_Search[256],
    		String_To_Find[256];
    
    	int iFound = 1;
    
    	int iPos = 0,
    		x = 0,
    		iFound_Pos,
    		Found_Char,
    		Search_Char;
    
    	cout<<"Enter a string to search\n";
    	cin.getline(String_To_Search, 255, '\n');
    	cout<<"\n";
    
    	cout<<"Enter a string to search for.\n";
    	cin.getline(String_To_Find, 255, '\n');
    	cout<<"\n";
    
    	if(strstr(String_To_Search, String_To_Find))
    	{
    		do
    		{
    			Search_Char = String_To_Find[x];
    			Found_Char = String_To_Search[iPos];
    			iPos++;	
    			iFound_Pos = iPos;
    
    			if(Search_Char == Found_Char)
    			{
    				x++;
    
    				if(x == (int)strlen(String_To_Find))
    				{
    					cout<<String_To_Find<<" found at position ";
    					cout<<iFound_Pos<<".";
    						iFound = 0;
    				}
    			}
    			else
    			{
    				x = 0;
    			}
    		}
    		while(iFound != 0);
    	}
    	else
    	{
    		cout<<String_To_Find<<" not found";
    	}
    	
    	return 0;
    }
    Last edited by WayTooHigh; 09-05-2001 at 08:15 AM.

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Question I dont think so

    I dont think you understand, the word "bye" is not a charachter, it is a whole string... a charachter would be 'b'.

    SPH

  5. #5
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    who are you talking to?

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb i

    The WaRped OnE

  7. #7
    It should be exactly what hes looking for...

    char *strstr( const char *string, const char *strCharSet );

    Return Value

    Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.

    Parameters

    string

    Null-terminated string to search

    strCharSet

    Null-terminated string to search for
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM