Thread: Help With String Word Problem, stuck thanks

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    5

    Help With String Word Problem, stuck thanks

    Hey I am new here and slightly new to C but I know alot of useful things, such as defining variables, some strings, loops, user defined functions, and arrays, also pointers and a few more things.

    My task is to write a C function that preforms a find within a string:

    1) Prompt the user for a test string(printf/scanf)
    2) Prompt the user for a string to find within the test string
    3)If the find string is found within the test string (Case or If right)?
    4) If the Find string is not found let the user no it was not found(default case right)?
    5) Prints out the number of times that the find string was found within the test case

    Assume case sensitivity, and maximum length is 100 chars


    Test Case: "this is a fun project to work on"
    fun found at column 11
    fun was found a total of 1 times

    Thanks I will be working on it in the comments below plz if you can help I know its probably easy for most people but it would really help me and be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main() {
        char str1[100];
        char str2[100];
        char *i;
     
     
        printf("Enter a test string: ");
        fgets(str1, 100, stdin);
        printf("Enter a string you want to find within the test string: ");
        fgets(str2, 100, stdin);
    
    
        for(i = 0; str2[i] != '\0'; i++) {
     
        if(p == NULL)
            printf("\nerror finding substring\n");
        else
            printf("\n%s\n",i);
     
        return 0;
    }
    a bit lost

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Magicman93
    My task is to write a C function that preforms a find within a string
    There is a standard library function named strstr that already does this, so are you supposed to write your own version of strstr, or are you supposed to say, use strstr to write a program that performs the find?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by laserlight View Post
    There is a standard library function named strstr that already does this, so are you supposed to write your own version of strstr, or are you supposed to say, use strstr to write a program that performs the find?
    I think we are supposed to use strstr but im not sure how it works.
    im still trying to figure it out can you help me?

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by laserlight View Post
    There is a standard library function named strstr that already does this, so are you supposed to write your own version of strstr, or are you supposed to say, use strstr to write a program that performs the find?
    I have to go to sleep now but This is what I have if you can modify it somehow to help me I would be so thankful
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
      char str[] = "String1 subString1 Strinstrnd subStr ing1subString";
      char sub[] = "subString";
    
      char *p1, *p2, *p3;
      int i=0,j=0,flag=0;
    
      p1 = str;
      p2 = sub;
    
      for(i = 0; i<strlen(str); i++)
      {
        if(*p1 == *p2)
          {
              p3 = p1;
              for(j = 0;j<strlen(sub);j++)
              {
                if(*p3 == *p2)
                {
                  p3++;p2++;
                } 
                else
                  break;
              }
              p2 = sub;
              if(j == strlen(sub))
              {
                 flag = 1;
                printf("\nSubstring found at index : %d\n",i);
              }
          }
        p1++; 
      }
    if(flag==0)
      {
           printf("Substring NOT found");
      }
    return (0);
     
    Im trying to set it so its user input and not set you know. 
    
    such as:
     printf("Enter a string:");
    scanf("%s", &str);
     printf("Enter a substring:");
    scanf("%s", &sub);
     also that that it 
    it says found a total of "ex 15 times")
    Thanks so much.
    

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    Strstr works like so
    Strstr (string, sub-string) ;
    It will only find the first occurrence of the sub string though. However it does not have to be used from the start of the string. Ie can be used to check any location in the string for a sub string.

    We had a similar task a few weeks ago.
    hope this helps. I'm still learning c so might not be the best answer

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, alien1 has the right idea: strstr will return a pointer to the first character of the substring found, or a null pointer if it could not be found. Therefore, you could write a loop like this:
    Code:
    char *haystack = str1;
    size_t needle_len = strlen(str2);
    while ((haystack = strstr(haystack, str2)) != NULL)
    {
        /* ... */
        haystack += needle_len; /* search the rest of the string */
    }
    Since you will be counting the number of times the "needle" is found in the "haystack", you can handle the case of "not found" and "found n times" together (i.e., n == 0 means "not found").
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I noticed your code in post #2 looks very similar to the code posted in a thread from yesterday: strcpy()

    This made me suspicious of your code in post #5 ... and hey, look, that was just copied as well: check substring exists in a string in C - Stack Overflow

    Copy/pasting code will not make you a programmer, any more than snapping photos will make you an artist.

    I suggested you start over, this time writing the code yourself.

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by Matticus View Post
    I noticed your code in post #2 looks very similar to the code posted in a thread from yesterday: strcpy()

    This made me suspicious of your code in post #5 ... and hey, look, that was just copied as well: check substring exists in a string in C - Stack Overflow

    Copy/pasting code will not make you a programmer, any more than snapping photos will make you an artist.

    I suggested you start over, this time writing the code yourself.
    I didnt say I was a programmer Im just trying to figure it out as an idea. Yes I used a code from yesterday to help me get started?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I didnt say I was a programmer...
    Well, you did say:

    Hey I am new here and slightly new to C but I know alot of useful things, such as defining variables, some strings, loops, user defined functions, and arrays, also pointers and a few more things.
    So it sounds like you are learning how to program.

    My point was that in order to learn, you have to do the work yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to reverse a string word by word?
    By SuperMiguel in forum C Programming
    Replies: 22
    Last Post: 03-29-2012, 12:40 AM
  2. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  3. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  4. Word from a string...
    By reRanger in forum C++ Programming
    Replies: 7
    Last Post: 11-22-2004, 07:24 AM
  5. Replies: 1
    Last Post: 04-01-2003, 06:02 AM

Tags for this Thread