Thread: [Help]C++
View Single Post
Old 11-16-2010   #10
ZC
Registered Member
 
ZC's Avatar
 
Last Online: 07-15-2013
Join Date: Aug 2006
Posts: 100
Thanks: 13
Thanked 24 Times in 24 Posts
Groans: 0
Groaned at 0 Times in 0 Posts
Default

Problem 3. Substrings.
Write a computer program which given two C-strings as input, checks if the first is a substring of the
second.
You are supposed to allow whitespaces in the input strings. You program should interpret a new line
as the end of first input string and similarly for the second input string.
Note that if s1[0 . . . l1 −1] and s2[0 . . . l2 −1] are character strings of lengths l1 and l2 respectively, we
say that s1 is a substring of s2 if there exists an integer i, 0  i  l2 −(l1 −1), such that s1[j] = s2[i+j]
for j = 0, . . . , l1 − 1
For example, “obl” is a substring of “Problem 2”, but “oblm” is not a substring of “Problem 2”.
Your program is supposed to give a YES/NO answer only.
Hint: You need two nested loops and boolean variable.
You are supposed to use the break statement to break the outer loop when the first occurrence of the
s1 in s2 is found (if any).
Note: To read two C-strings using cin.get, use an auxiliary variable to absorb the newline after
reading the first string (as otherwise the second string will be set to the empty string due the the newline
remaining in the input stream .... something annoying in C++
__________________
Books all say different things while people flap their yellow wings trying
to soar by being a whore of life!!!!




ZC is offline   Reply With Quote