Strings and Substrings using arrays

This is a discussion on Strings and Substrings using arrays within the C Programming forums, part of the General Programming Boards category; I am having troubles with setuping the next part of the question. Also I am not exactly sure if my ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Strings and Substrings using arrays

    I am having troubles with setuping the next part of the question. Also I am not exactly sure if my scanning parts for strings are correct. I'm trying get the using to input a 4 character string (s1), then another string less than 4 characters
    (s2) to determine if s2 is a substring of s1.

    here is my code so far:

    Code:
    #include <stdio.h>
    int main()
    {
    
    	char s1[4];
    	char s2[4];
    
    	int i;
    
    	
    	printf("\nEnter three character word for string1: ");
    	scanf("%s", s1[]);
    
    	printf("\nEnter three characters or less to check for substring in string 1: ");
    	scanf("%s", s2[]);

    My example that I was going to test was to test the word "CALL" and then s2 would be "ALL".

    My problem is to figure out how to get the arrays to notice that the substring s2 is the same as s1. The same characters in the same pattern and direction.

    Can anyone help me out, thanks!

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    I was thinking about it, and would a for loop to scan if the arrays of the s2 is the same as the arrays in the s1. I suppose I would need a nested for loop in this case?

    BTW, I can only use the library functions printf and scanf

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I see no reason why you couldn't make a nested for loop work.

    Think about how you would determine whether one string is a substring of another. And try not to abstract yourself away (in other words, if your answer is "I read it", you need to break that down further).

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    I would examine each character of the first string...then I would examine each character of the substring, then see if one of the characters match (I suppose from left to right). Then I would do the same process for the next character...

    Well, here is in better details:

    I would probably assign each character to a specific array for string 1 and string 2. I would then examine the substring's first character and then try and match it up with the strings. Then for the subtrings second character, it would have to be right after the character of the first string.
    Once I don't see anymore matches, then the process is complete

    Does that sound reasonable?

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by dcwang3 View Post
    I would examine each character of the first string...then I would examine each character of the substring, then see if one of the characters match (I suppose from left to right). Then I would do the same process for the next character...

    Well, here is in better details:

    I would probably assign each character to a specific array for string 1 and string 2. I would then examine the substring's first character and then try and match it up with the strings. Then for the subtrings second character, it would have to be right after the character of the first string.
    Once I don't see anymore matches, then the process is complete

    Does that sound reasonable?
    Absolutely.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dcwang3 View Post
    I would probably assign each character to a specific array for string 1 and string 2.
    I have no idea what you mean by this. I'm pretty sure it doesn't matter though.

    Quote Originally Posted by dcwang3
    I would then examine the substring's first character and then try and match it up with the strings.
    Good start -- but remember you can only look at one character at a time (from each string). You're "reading" again; you'll have to be way more explicit about "try and match it up".

    Quote Originally Posted by dcwang3
    Then for the subtrings second character, it would have to be right after the character of the first string.
    Right.

    Quote Originally Posted by dcwang3
    Once I don't see anymore matches, then the process is complete

    Does that sound reasonable?
    There are three ways to run out of matches: running out of letters in the substring, running out of letters in the string, and finding two letters in position that don't match. You'll have to deal with each separately.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    what is the best way and easiest way to do this then?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dcwang3 View Post
    what is the best way and easiest way to do this then?
    You're headed in the right direction. You just need to be ready to break those steps down to the point where you can actually get them in a program.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    Quote Originally Posted by Elysia View Post
    so the way I did it is incorrect, or it is correct but shouldn't be used?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    The point is that the *scanf family of functions can be difficult to use if you aren't reading a format. All of scanf's problems are caused by the unreliability of user input. User's can make mistakes and be careless, so programs ready for the market are often written differently to prevent things like typing strings that are too long for arrays of a certain size and so forth.

    At a minimum, the *scanf family of functions should be passed a thorough format string. In the case of reading whitespace separated words, include a width for &#37;s.

    this could look like
    Code:
    #include <stdio.h>
    
    #define ARRAY_MAX 100
    
    
    /* ... */
    
    char t[ARRAY_MAX];
    scanf( "%99s", t );
    Use what you must to get a grade, but we like to encourage people to write safer programs, and expose them to safer methods. Like replacements for gets and scanf("%s" /*...*/).
    Last edited by whiteflags; 02-17-2008 at 08:19 PM.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    can someone tell me what the appropriate way to get the user's input of string characters?

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Use fgets for it. Theres an example of how to use it in the faq.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 10:52 AM
  3. How to compare substrings with strings?
    By AsleeKaizoku in forum C Programming
    Replies: 13
    Last Post: 02-26-2008, 04:57 PM
  4. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  5. Searching strings - substring's
    By Vber in forum C Programming
    Replies: 4
    Last Post: 02-06-2003, 11:05 AM

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