Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,405 members, 7,836,620 topics. Date: Wednesday, 22 May 2024 at 10:39 AM

Contest[closed] : Program A Function to find if a phrase. is in a string - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Contest[closed] : Program A Function to find if a phrase. is in a string (3763 Views)

Php Function To Generate A Random Expression / User Defined Function To Validate Regular Expression In Javascript / I Can Program A Html Css Javascript Website For 1k (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 5:13pm On Aug 02, 2017
Omo, the kind of codes flying about here na be am o
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 5:17pm On Aug 02, 2017
dhtml18:
Omo, the kind of codes flying about here na be am o

Huh?
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by jidez007: 5:23pm On Aug 02, 2017
Solution in C++

Got c++ exam tomorrow so I may as well write in c++


#include <iostream>

int len_str(char s[]) {
int i = 0;
while (1) {
if(s[i] == NULL)
break;
i++;
}
return i;
}

bool cmp_char(char s[], char c[]) {

if(len_str(s) != len_str(c)) { // different lengths
return false;
}

for(int i = 0; i < len_str(s); i++) {
if(s[i] != c[i])
return false;
}
return true;
}

bool find(char word[], char sentence[]) {
int word_len = len_str(word);
int sen_len = len_str(sentence);

if(word_len > sen_len) {
std::cout << "warn: word to find is greater than sentence" << std::endl;
return false;
}

char temp[word_len+1];

for(int i = 0; i <= sen_len - word_len; i++) {

for(int j = 0; j < word_len; j++) {
temp[j] = sentence[i+j];
}
// terminate temp word
temp[word_len] = NULL;

if(cmp_char(word, temp)) {
std::cout << "From position " << i << " to " << i + word_len << std::endl;
return true;
}
}

return false;
}

int main() {
char sentence[] = "the boy is good";
char word[] = "boy";

if(find(word, sentence)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;

}
return 0;
}
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 5:24pm On Aug 02, 2017
CryptoCoinr:
I'm more of an amateur than a newbie (sort of) but it took longer than I expected, and more iterations than I anticipated (still have them in Sublime Text), but I finally did it. This is in JavaScript:

[-- former presentation of code --]

You can test it yourself with your browser (Chrome; any browser really but the shortcut is for Chrome) if you're using a PC.

- Press Ctrl + Shift + J in any open tab
- Copy and Paste the above code into the console and hit the Return/Enter key
- Type find("whatever word you want to find", "whatever sentence you want the word to be found in" ) into the console and hit the Return/Enter key.

I've figured out the BB Code for code so here's my JS submission again, with indentation undestroyed:



function find(word, text)
{
let index = 0;
let lastIndex = 0;
let startIndex = 0;
let placeholder = "";

while (startIndex < text.length && placeholder !== word)
{
lastIndex = startIndex + (word.length - 1);

if (word[index] === text[startIndex] && word[word.length - 1] === text[lastIndex])
{
for (startIndex; startIndex <= lastIndex; startIndex++)
{
if (word[index] === text[startIndex])
{
placeholder+= word[index];
index++;
}
else
{
index = 0;
placeholder = "";
startIndex = lastIndex;
}
}
}
else
{
startIndex++;
}
}

return (placeholder === word) ? 1 : 0;
}



To preserve indentation, wrap your code with [ code ] actual code goes here... [/ code ] instead of [ quote ] [/ quote ] (no spaces between the [, "code" and ]).
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by greatface(m): 5:24pm On Aug 02, 2017
Implemented in python 3

def length(s):
l=0
while s:
l+=1
return l

def findphrase(p,s):
sl = length(s)
pl = length(p)
fpl = 0 #found phrase length
cpi = 0 #current phrase index
csi = 0 #current sentence index
while sl:
if fpl == pl: return 1
elif p[cpi] == s[csi]:
fpl += 1, csi += 1, cpi += 1, sl - 1
else:
fpl = 0, cpi = 0, sl - 1

phrase = input('enter phrase: ')
sentence = input('enter sentence: ')
print(findphrase(phrase, sentence))
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by orimion(m): 8:23pm On Aug 02, 2017
Haskell


words' :: String -> [String]
words' "" = []
words' s = transform s "" []
where
transform "" cur acc = (if cur /= "" then cur:acc else acc)
transform (x:xs) cur acc =
case x of
' ' -> transform xs "" (if cur /= "" then cur:acc else acc)
_ -> transform xs (cur ++ [x]) acc

finder :: String -> [String] -> Int
finder _ [] = 0
finder needle (x:haystack) = if x == needle then 1 else (finder needle haystack)

find :: String -> String -> Int
find needle haystack = finder needle (words haystack)


the first two functions (words' and finder) are helper functions
there is a builtin "words" function that does what " words' " did hence the quote

Normally, we wouldn't return an Int we would return a Maybe Int but as per the spec..
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 9:50am On Aug 03, 2017
closed testing result winners will be announced once done
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by edicied: 12:13pm On Aug 03, 2017
I no even understand the question self grin Op are you saying in a String for example "The boy is here" we should check for if the words "The" "BOy" or "Here" is there?
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by greatface(m): 1:00pm On Aug 03, 2017
greatface:
Implemented in python 3

def length(s):
l=0
while s:
l+=1
return l

def findphrase(p,s):
sl = length(s)
pl = length(p)
fpl = 0 #found phrase length
cpi = 0 #current phrase index
csi = 0 #current sentence index
while sl:
if fpl == pl: return 1
elif p[cpi] == s[csi]:
fpl += 1, csi += 1, cpi += 1, sl - 1
else:
fpl = 0, cpi = 0, sl - 1

phrase = input('enter phrase: ')
sentence = input('enter sentence: ')
print(findphrase(phrase, sentence))
Looking At My Code Now I Saw A Lot Of Programming And Logical Errors. Well, I Did That With My Chinco Phone Meant For Calls.

I Will Post The Updated version later.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by shittu123(m): 1:11pm On Aug 03, 2017
def lenght(x):
count = 0;
for r in x:
count +=1;
return count;

def findString(word,Sentence):
count = 0;
xlength = lenght(word);
ySplit = Sentence.split();
ySplit_len = lenght(ySplit);
while count <= ySplit_len:
if count == ySplit_len:
break;
if x in ySplit:
return word
else:
return "'{}' not Found".format(word.upper());

count += 1;
x = input("Enter the Word : "wink;
y = input("Enter the Sentence containing the '{}' : ".format(x.upper()));
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 4:02pm On Aug 03, 2017
shittu123:
def lenght(x):
count = 0;
for r in x:
count +=1;
return count;

def findString(word,Sentence):
count = 0;
xlength = lenght(word);
ySplit = Sentence.split();
ySplit_len = lenght(ySplit);
while count <= ySplit_len:
if count == ySplit_len:
break;
if x in ySplit:
return word
else:
return "'{}' not Found".format(word.upper());

count += 1;
x = input("Enter the Word : "wink;
y = input("Enter the Sentence containing the '{}' : ".format(x.upper()));


No in-built functions
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 5:59pm On Aug 03, 2017
[quote author=justanALIAS post=59077579]
I dont think so but i will try. By the way, the above code is buggy; see d fixed version:


package main

import (
"fmt"
)
func length(dstring string) int { // i made my own len() method
var counter int
for range dstring {
counter++
}
return counter
}
func main() {
mainstr := "i am a bon boni"
substr := "i am"
fmt.Println(check(mainstr, substr))
}
func check(mainstr string, substr string) int{
var status bool = false
for i, _ := range mainstr {
if mainstr[i] == substr[0] {
if length(mainstr[i:]) >= length(substr) {
last := len(substr)
di := i + last
var rangeof string = mainstr[i:di]
if substr == rangeof{
status = true
} else {
continue
}
} else {
continue
}
}
}
if status {
return 1
} else {
return 0
}
}

[/quote




NICE WORK BUT your code

returns 1 when "i am" matched against "i ammy"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 6:01pm On Aug 03, 2017
[quote author=silento post=59118512][/quote]
Dnt get
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:03pm On Aug 03, 2017
edicied:
I no even understand the question self grin Op are you saying in a String for example "The boy is here" we should check for if the words "The" "BOy" or "Here" is there?


the program will able to match a phrase

like "under it" if matched to " my bed is under it but am cool"


it will return 1 to show that match was found


but 0 when "under" matched to"my bed is underneath it"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:06pm On Aug 03, 2017
justanALIAS:

Dnt get

the program is expected to return 1 in this case:

"i am" => to "i am a boy"


and 0 in this case :

"i am" => "i ammy is a boy"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 6:06pm On Aug 03, 2017
silento:



the program will able to match a phrase

like "under it" if matched to " my bed is under it but am cool"


it will return 1 to show that match was found


but 0 when "under" matched to"my bed is underneath it"

I noticed this but I don't think you clarified it in the OP so I'm working on a fix for my solution now.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 6:06pm On Aug 03, 2017
silento:



the program will able to match a phrase

like "under it" if matched to " my bed is under it but am cool"


it will return 1 to show that match was found


but 0 when "under" matched to"my bed is underneath it"
Oooo grin grin ... I thought as long as the string is present..
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:10pm On Aug 03, 2017
jidez007:
Solution in C++

Got c++ exam tomorrow so I may as well write in c++


#include <iostream>

int len_str(char s[]) {
int i = 0;
while (1) {
if(s[i] == NULL)
break;
i++;
}
return i;
}

bool cmp_char(char s[], char c[]) {

if(len_str(s) != len_str(c)) { // different lengths
return false;
}

for(int i = 0; i < len_str(s); i++) {
if(s[i] != c[i])
return false;
}
return true;
}

bool find(char word[], char sentence[]) {
int word_len = len_str(word);
int sen_len = len_str(sentence);

if(word_len > sen_len) {
std::cout << "warn: word to find is greater than sentence" << std::endl;
return false;
}

char temp[word_len+1];

for(int i = 0; i <= sen_len - word_len; i++) {

for(int j = 0; j < word_len; j++) {
temp[j] = sentence[i+j];
}
// terminate temp word
temp[word_len] = NULL;

if(cmp_char(word, temp)) {
std::cout << "From position " << i << " to " << i + word_len << std::endl;
return true;
}
}

return false;
}

int main() {
char sentence[] = "the boy is good";
char word[] = "boy";

if(find(word, sentence)) {
std::cout << "Found" << std::endl;
} else {
std::cout << "Not Found" << std::endl;

}
return 0;
}


NICE WORK BUT return 1

when matched to "the aboyu is a man"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:12pm On Aug 03, 2017
justanALIAS:

Oooo grin grin ... I thought as long as the string is present..
presence mean "boy" not "aboyoy"

the two word are different
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:18pm On Aug 03, 2017
CryptoCoinr:


I noticed this but I don't think you clarified it in the OP so I'm working on a fix for my solution now.


i don't to clarify it because it is so obvious

in English is you spell boy as aboy is not a boy again

cool
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by orimion(m): 6:19pm On Aug 03, 2017
The original question says the first parameter is a word

silento:

so the function will do the following

when given a two parameters the first will be a word and second will be a sentence

it should be able to detect that the word is in the sentence example

function find("boy","obi is a boy"wink
{
code........

return 0 or 1
}

the function will return 0 or 1
0 if word not found in the sentence
1 if word is found in the sentence

But here, it is a phrase

silento:


the program is expected to return 1 in this case:

"i am" => to "i am a boy"


and 0 in this case :

"i am" => "i ammy is a boy"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 6:29pm On Aug 03, 2017
silento:



i don't to clarify it because it is so obvious

in English is you spell boy as aboy is not a boy again

cool

Um, no it isn't. As orimion said above, your question was unclear and even the inbuilt functions of various languages, or JS at least, finds "words" whether they are fragments or solitary.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:31pm On Aug 03, 2017
orimion:
The original question says the first parameter is a word



But here, it is a phrase


if you are on a computer

open up your browser


and press ctrl+f now type "word" and click the button for whole words


did you browser highlighted "words" to be "word" if it didn't then i think you got your answer

we are here to learn you will only find examples all over the internet with this bug of matching "boy" to be same with "boym"
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:36pm On Aug 03, 2017
CryptoCoinr:


Um, no it isn't. As orimion said above, your question was unclear and even the inbuilt functions of various languages, or JS at least, finds "words" whether they are fragments on solitary.


that's true but that why regular expressions sell like water
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 6:40pm On Aug 03, 2017
silento:



that's true but that why regular expressions sell like water

Oh yes, I'm aware of regex and its awesome text manipulation capabilities but AFAIK, you need to use JS's inbuilt functions if you want to go the regex route.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by orimion(m): 6:54pm On Aug 03, 2017
silento:


if you are on a computer

open up your browser


and press ctrl+f now type "word" and click the button for whole words


did you browser highlighted "words" to be "word" if it didn't then i think you got your answer

we are here to learn you will only find examples all over the internet with this bug of matching "boy" to be same with "boym"




I don't think you understand me

I was making the distinction between a word and a phrase

in the question spec, the example given was

"boy" & "i am a boy" should return 1
(i.e word and sentence)

but the part i quoted earlier,

"i am" & "i am a boy"
(i.e phrase and sentence)

I pointed this out because in my solution, i split the sentence into a list of words first (as per the question) then check if the word is in the list
obviously it's not going to work in the second situation (phrase & sentence)

PS. as for the challenge, chrome doesn't have "match whole word" and so highlighted "words" for "word"
PPS. did you even try my haskell solution
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by mustaphagreens(m): 6:55pm On Aug 03, 2017
silento:
so the function will do the following

when given a two parameters the first will be a word and second will be a sentence

it should be able to detect that the word is in the sentence example
Mr silento,
Please could you enlighten me on the problem with my code?
Your problem specification states:
Function should return:
0 if word is not a subset of the sentence
1 if otherwise. That's precisely what findString was constructed for.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 7:15pm On Aug 03, 2017
orimion:


I don't think you understand me

I was making the distinction between a word and a phrase

in the question spec, the example given was

"boy" & "i am a boy" should return 1
(i.e word and sentence)

but the part i quoted earlier,

"i am" & "i am a boy"
(i.e phrase and sentence)

I pointed this out because in my solution, i split the sentence into a list of words first (as per the question) then check if the word is in the list
obviously it's not going to work in the second situation (phrase & sentence)

PS. as for the challenge, chrome doesn't have "match whole word" and so highlighted "words" for "word"
PPS. did you even try my haskell solution


no
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 7:30pm On Aug 03, 2017
I hope you guys learned a lot from this tiny problem

if some company paid you to wrote a code for them like this one and a after you code has this bug kiss it goodbye I know alot of dudes on nl that will know that matching any occurrence is a bug


if you think I cheated you guys go to stackoverflow and you will see that 86% of there code has this bug while alot of devs there have solved it which others stick with inbuilt buggy function


please check the first page and drop your number if your name is mentioned
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by mustaphagreens(m): 7:33pm On Aug 03, 2017
silento:
I hope you guys learned a lot from this tiny problem

if some company paid you to wrote a code for them like this one and a after you code has this bug kiss it goodbye I know alot of dudes on nl that will know that matching any occurrence is a bug


if you think I cheated you guys go to stackoverflow and you will see that 86% of there code has this bug while alot of devs there have solved it which others stick with inbuilt buggy function


please check the first page and drop your number if your name is mentioned

Could you please provide answer to the question I asked earlier?
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 7:35pm On Aug 03, 2017
silento:
I hope you guys learned a lot from this tiny problem

if some company paid you to wrote a code for them like this one and a after you code has this bug kiss it goodbye I know alot of dudes on nl that will know that matching any occurrence is a bug


if you think I cheated you guys go to stackoverflow and you will see that 86% of there code has this bug while alot of devs there have solved it which others stick with inbuilt buggy function

I'm pretty sure an interviewer for a job would be clearer than you are with the specifications.

3 Likes 1 Share

Re: Contest[closed] : Program A Function to find if a phrase. is in a string by edicied: 8:07pm On Aug 03, 2017
Another challenge please am loving this

(1) (2) (3) (Reply)

Updated 2017 | 2018 100% Working Freebitco Script - Earn 0.05BTC / Please Note Only For Programmers.. / Companies not giving IT Projects to Nigerians

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 76
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.