Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,107 members, 7,814,884 topics. Date: Wednesday, 01 May 2024 at 10:04 PM

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

Nairaland Forum / Science/Technology / Programming / Contest[closed] : Program A Function to find if a phrase. is in a string (3742 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)

Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 12:11am On Aug 01, 2017
just a simple but powerful function to improve our coding skills


beginners onlys

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


contest is closing on 3rd of this month

Note : you are not allowed to use any inbuilt library or functions just from scratch code every thing




some time similar to str.find()

in python


any language is welcomed but c and c ++ Will be more interesting






pros are not welcomed

just New coders




SINCE WE DO NOT HAVE A WINNER

I WILL LIKE TO Honor THIS GREAT PROGRAMMERS

1.justanALIAS (golang)

2.jidez007 (c++)


please drop your numbers for a little appreciation cheesy wink









SOLUTION IN C language





#include <stdio.h>



//FUNCTION THAT CHECKS if a phrase exists in sentence

int found(char* phrase,char *sentence)
{
//one line function to check lenght
int len(char*item){int t=0;while(item[t] !='\0')t++;return t;}

// ckecking length
int sent_len = len(sentence);
int word_len = len(phrase);

//variables for keeping records and also indexing through string array
int c=0,j=0,chk=0;

//for loop to loop through sentence
for(c=0;c<sent_len;c++)
{


//check if phrase[0] is equal to sentence[c]
if(phrase[j]==sentence[c])
{
/*if sentence[c-1] is not a whitespace ,and also j==0 and c not ==0
that mean that the phrase is just similar word like 'to' and 'toy' to avoid it such scenario this if statement is must
*/

if(sentence[c-1] !=32 & j==0 & c !=0)
{
break;
}

//if above evaculation is false then this one increment this record variables
else
{
chk+=1;
j+=1;
}


}

/*
while matching word like 'toma' against 'toys' once it match 'm' to 'y'
it will reset record variable to null and it means not matched

*/
else
{
chk=0;
j=0;
}


//if chk record variable is equal to phrase lenght then break loop because a match found
if((chk==word_len)&(sentence[c+1]==32))
{

break;
}



}

if(chk==len(phrase))
return 1;
else
return 0;

}


int main()
{

char r[]="boy are goo";
char t[] = "d boy you boy are goo d";
printf("%d ",found(r,t));
return 0;
}



Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 12:13am On Aug 01, 2017
3days for the contest to run
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 1:05am On Aug 01, 2017
And of course I will be umpire. . .
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Desyner: 1:14am On Aug 01, 2017
Clarify please. Custom functions that don't call library function?
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Jaddo19(m): 1:19am On Aug 01, 2017
I Really Dont Understand D Question Fully, Can U Explain Better Pls
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:42am On Aug 01, 2017
Jaddo19:
I Really Dont Understand D Question Fully, Can U Explain Better Pls

I hope you understand it now
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:43am On Aug 01, 2017
Desyner:
Clarify please. Custom functions that don't call library function?

just a custom function but no library or inbuilt function
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Welete(m): 7:46am On Aug 01, 2017
#python 3.x
def wordsearch(x, sent):
for x in sent:
return 1
break
else: return 0

x = input('word: ')
sent = input('sentence: ')
wordsearch(x, sent)
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Welete(m): 7:51am On Aug 01, 2017
Welete:
#python 3.x
def wordsearch(x, sent):
for x in sent:
return 1
break
else: return 0

x = input('word: ')
sent = input('sentence: ')
wordsearch(x, sent)

nairaland is messing with my indentation.. cry
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Jaddo19(m): 8:22am On Aug 01, 2017
Using Php
.
.
<?Php
If (strpos("is This What You Want","What"wink){
Echo true;
}else{
Echo (int)false;
}
?>
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 8:28am On Aug 01, 2017
Jaddo19:
Using Php
.
.
<?Php
If (strpos("is This What You Want","What"wink){
Echo true;
}else{
Echo (int)false;
}
?>


great but no inbuilt function or library and fancy operator

https://msdn.microsoft.com/en-us/library/2hxce09y(v=vs.90).aspx?_e_pi_=7%2CPAGE_ID10%2C1207831521
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 8:29am On Aug 01, 2017
Welete:


nairaland is messing with my indentation.. cry


no fancy operator
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 8:30am On Aug 01, 2017
Welete:
#python 3.x
def wordsearch(x, sent):
for x in sent:
return 1
break
else: return 0

x = input('word: ')
sent = input('sentence: ')
wordsearch(x, sent)


in operator a membership operator only in python I consider it inbuilt function
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 8:33am On Aug 01, 2017
dhtml18:
And of course I will be umpire. . .


the troll master
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 9:41am On Aug 01, 2017
package main

import(
"fmt"
"strings"
)
func main(){
if check("justanALIAS", "justan") == 1 {
fmt.Println("success")
}else {
fmt.Println("failure")
}
func check(main string, sub string) int {
if strings.Contains(main, sub){
return 1
}else {
return 0
}
}
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 9:43am On Aug 01, 2017
justanALIAS:
package main

import(
"fmt"
"strings"
)
func main(){
if check("justanALIAS", "justan"wink == 1 {
fmt.Println("success"wink
}else {
fmt.Println("failure"wink
}
func check(main string, sub string) int {
if strings.Contains(main, sub){
return 1
}else {
return 0
}
}
Golang
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 1:46pm On Aug 01, 2017
golang hard ooo
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 2:02pm On Aug 01, 2017
silento:
golang hard ooo
grin not true oo
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by mustaphagreens(m): 2:18pm On Aug 01, 2017
Python 3.5
Check the attached image

1 Like

Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 6:46pm On Aug 01, 2017
python user without the help of in operator rewrite you code
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 6:50pm On Aug 01, 2017
^^^Na wa o!
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Olyboy16(m): 10:43pm On Aug 01, 2017
in Kotlin



//using std library
var haystack = "hello NL geeks"
assertEquals("geek", haystack.contains("geek" ) )

//OR
assert("NL" in haystack)

Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 10:59pm On Aug 01, 2017
^^^Yah!
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 8:51am On Aug 02, 2017
mustaphagreens:
Please check the attached images. Nairaland clearly does not allow for indentation.

Language: Python
Version: 3.0
Using the in operator
def findString(x,y):
if x in y:
return 1
else:
return 0

#an instance of findString
print(findString("boy","I am a boy" ))



Done!!!
Check out the code below or in the second image
#findString without the in operator

def findString(x,y):
i=0
p=""
while i<=len(y)-1:
if (y[i]!=" " and p!=x):
p+=y[i]
i+=1
elif p==x:
return 1
else:
i+=1
p=""
if p==x:
return 1
else:
return 0

#client program invoking findString
x=input("enter target word:" )
y=input("enter phrase or sentence:" )
print(findString(x,y))

wow that's great nice indentation in your code

But can you rewrite the code so that if Len()

will not be there

the essence of this contest is to make you understand what happen beneath the standard libraries and function

anyway you will do great things in python keep on trying you got the heart of a coded indeed
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 10:07am On Aug 02, 2017
Sorry just seeing this, I will post mine the goal is no inbuilt function and yet people still using contains and strpos this is why Nigerians fail algorithm test, even the interview we conducted we spelt it out loud and yet still the same thing.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 10:39am On Aug 02, 2017
pcguru1:
Sorry just seeing this, I will post mine the goal is no inbuilt function and yet people still using contains and strpos this is why Nigerians fail algorithm test, even the interview we conducted we spelt it out loud and yet still the same thing.


nawa the tire me ooo
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 12:29pm On Aug 02, 2017
package main

import (
"fmt"
)
// i made my own len() method cool
func length(dstring string) int {
var counter int
for range dstring {
counter++
}
return counter
}
func main() {
mainstr := "i am a bon boi"
substr := "boi"
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] {
last := length(substr)
di := i + last
if substr == mainstr[i:di] {
status = true
} else {
continue
}
}
}
if status {
return 1
} else {
return 0
}
}
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 1:12pm On Aug 02, 2017
justanALIAS:
package main

import (
"fmt"
)
// i made my own len() method cool
func length(dstring string) int {
var counter int
for range dstring {
counter++
}
return counter
}
func main() {
mainstr := "i am a bon boi"
substr := "boi"
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] {
last := length(substr)
di := i + last
if substr == mainstr[i:di] {
status = true
} else {
continue
}
}
}
if status {
return 1
} else {
return 0
}
}
range
is inbuilt operator in golang can you scrap it
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by Nobody: 2:22pm On Aug 02, 2017
silento:

range
is inbuilt operator in golang can you scrap it
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
}
}
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by silento(m): 3:03pm On Aug 02, 2017
justanALIAS:

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
}
}



ok
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 4:15pm On Aug 02, 2017
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:


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;
}

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.
Re: Contest[closed] : Program A Function to find if a phrase. is in a string by CryptoCoinr(f): 4:56pm On Aug 02, 2017
OK. Here are the shortcuts for all (most) browsers to open the console:

Chrome and Firefox - Ctrl + Shift + J
Edge and IE - F12, then select "Console"

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

What U Need To Know Before Learn A Programming Language / It's easy, learn a new tech skill. / What Do You Think Of This Program I Wrote

(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. 50
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.