I need help with creating a substring program

This is a discussion on I need help with creating a substring program within the C Programming forums, part of the General Programming Boards category; Hello my teacher told me to create a program that returns the substring of a string given the starting index ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    I need help with creating a substring program

    Hello my teacher told me to create a program that returns the substring of a string given the starting index of the substring and its length. For example, the user has entered the string "The quick brown fox" and has entered 7 as the starting index of the substring and 5 as the length. Then, the program should return the substring "brown". Write a function named substring that returns a pointer and accepts a pointer and 2 integers. The first integer refers to the starting index of the substring in the text and the second the length of the substring. Problem is that I am still a begginer in c and he did not teach us anyhing about substrings. Could you please help me out by telling me where to begin and how to progress. Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by CProgramingBegg
    Problem is that I am still a begginer in c and he did not teach us anyhing about substrings.
    Looks like he taught you something about substrings: a substring is some portion of a string.

    So, you have been taught loops, right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Looks like he taught you something about substrings
    Yes he has taught us about loops. That was part of the instructions, he didnt teach us any method of solving for it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    hmm... so you wanted your teacher to specify the algorithm for you? Good thing that he did not, because it gives you a chance to come up with one yourself

    For starters, write a function that prints the string character by character. That is, instead of just using puts() or printf() with a %s format specifier, use a loop with putchar(). Show us the function so that we can see that you have some idea how to loop over the characters of a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    start by just printing the substring.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void substring(char *s, int start, int n);
    
    int main()
    {
        char *s = "the quick brown fox";
        substring(s, 10, 5);
        return EXIT_SUCCESS;
    }
    
    void substring(char *s, int start, int n)
    {
        int i;
        for (i = 0; i < n; i++) putchar(s[start++]);
        putchar('\n');
    }

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        char userarray[];
        int i;
        
        i = 0;
        
        printf("Hello! Please input a string that is 7 characters long");
        printf("%s", &userarray[6]);
        
        while (i <= 7);
        {
              printf("%s", userarray[i]);
        }
        
    getch();
    return 0;
    }
    This is my code. I only know very little about c. I'm sure this is not sufficient enough, can you please guide me on what to do next? Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Have you tried compiling your program yet? You did not specify the array size.

    A for loop, as in Meldreth's example, is probably better than a while loop here (considering that you forgot to increase the value of i). Other problems include the fact that you are not correctly reading into the string (you are reading into the string from the character at index 6 onwards, but you probably wanted to read it int from the character at index 0). Your while loop also has a terminating semi-colon that should not be there.

    Incidentally, unless you are required to use it, remove the call to getch(), after which you can remove the #include <conio.h>. getch() is non-standard is not necessary here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    You used printf here instead of scanf:
    Code:
    printf("%s", &userarray[6]);
    It should be, once you've properly sized your array, as laserlight said, like so:
    Code:
    scanf("%s", userarray);

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Sorry I forgot, my Os is vista ultimate and for some reason it wont compile .c files. I tried researching for .c compilers for vista on google, I tried some of them but they wouldn't work. Do you know of any .c compilers that work on vista ultimate 32x?

    After I create a loop that prints the contents of the string one by one, what should I do next?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by CProgramingBegg
    Sorry I forgot, my Os is vista ultimate and for some reason it wont compile .c files. I tried researching for .c compilers for vista on google, I tried some of them but they wouldn't work. Do you know of any .c compilers that work on vista ultimate 32x?
    What compiler were you trying to use? Are you sure that it is a problem running the compiler and not a compile error reported by the compiler?

    Quote Originally Posted by CProgramingBegg
    After I create a loop that prints the contents of the string one by one, what should I do next?
    Modify the loop such that you print starting from the first character of the substring until the end of the substring.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem creating program to the specific specification
    By rushhour in forum C++ Programming
    Replies: 22
    Last Post: 11-27-2008, 11:15 PM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21