Thread: char pointer with for loops

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    11

    char pointer with for loops

    Hi, I need help with this please.
    I am trying to get the function to determine if the second string entered by the user is contained in the first string by defining the substr() function. I was able to achieve it with a while loop and a nested if statement. I was wondering if someone could help me figure out how to do it with a nested for loop. The prototype is given below and i am using eclipse. Thanks!


    Code:
    #include <stdio.h>
    #include <string.h>
    // returns the starting index of the first such occurrence
    // where sub_str was found within str - if one exists
    // otherwise returns -1
    int substr(const char str[], const char sub_str[]);
    int main()
    {
    	char str[255];
    	printf("Enter a string: "); fflush(stdout);
    	scanf("%s", str);
    	char sub_str[255];
    	do
    	{
    		printf("Enter a string to search for (\"quit\" to quit): "); fflush(stdout);
    		scanf("%s", sub_str);
    		if ( strcmp(sub_str, "quit") )
    		{
    			int ind = substr(str, sub_str);
    			if ( ind == -1 )
    			{
    				printf("Not found: %s\n", sub_str);
    			}
    			else
    			{
    				printf("\"%s\" found at starting index %d\n", sub_str, ind);
    			}
    		}
    	} while ( strcmp(sub_str, "quit") );
    	printf("Done!\n");
    	return 0;
    }
    Last edited by bab27; 10-01-2019 at 11:14 PM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,523
    Are you able to show the implementation of substr() where you achieved it using "a while loop and a nested if statement"?

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    11
    I was able to implement it with a while loop and nested if but i do not have access to code now.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    680
    @bab27

    I would use fgets() to input the string rather than use scanf() as scanf() as written inputs a word not a string.

    fgets() does input the newline at the end of the string input. You would need to remove the newline if you want to compare against "quit".

    Or you could just ask for, and see if the first char is a 'q'.
    Last edited by rstanley; 10-02-2019 at 07:37 AM.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    11
    It ran fine when i used scanf() but i will try with fgets(). Thanks for the tip!

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    555
    Is it necessary to implement via loop? You could do this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    static char *readstring( char *, char ** );
    
    int main( void )
    {
      char *s1, *s2, *s3;
      size_t size1, size2;
    
      if ( ! readstring( "Enter string: ", &s1 ) )
      {
        fputs( "ERROR: Cannot read string.\n", stderr );
        return EXIT_FAILURE;
      }
    
      if ( ! readstring( "Enter substring: ", &s2 ) )
      {
        free( s1 );
        fputs( "ERROR: Cannot read substring.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // strstr() do what you want!
      if ( s3 = strstr( s1, s2 ) )
      {
        size_t index = s3 - s1;
    
        printf( "Found substring at index %zu.\n", index );
      }
      else
        puts( "Substring not found." );
    
      free( s1 );
      free( s2 );
    
      return EXIT_SUCCESS;
    }
    
    char *readstring( char *prompt, char **sptr )
    {
      char *p;
      size_t size;
    
      // Prints the prompt...
      fputs( prompt, stdout );
      fflush( stdout );
    
      // Reads a line, dynamically allocating as many memory as necessary.
      *sptr = NULL;
      size = 0;
      if ( getline( sptr, &size, stdin ) == -1 )
        return NULL;  // returns NULL in case of error.
    
      // Get rid of '\r' or '\n' at the end of the string.
      if ( p = strpbrk( *sptr, "\r\n" ) )
        *p = '\0';
    
      return *sptr;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning a char value to char pointer doesn't work.
    By inckka in forum C Programming
    Replies: 5
    Last Post: 03-15-2017, 04:34 AM
  2. Replies: 2
    Last Post: 12-02-2012, 05:25 AM
  3. Read char by char into two-dimensional array pointer.
    By 'Serj Codito in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2012, 09:47 AM
  4. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  5. value of char variable with loops
    By david999 in forum C Programming
    Replies: 5
    Last Post: 11-03-2005, 01:55 PM

Tags for this Thread