php editor Strawberry will introduce how to store string tags into an array. During programming, we often need to process strings and split them into multiple tokens. Storing these tags in an array makes it easier for us to process and operate on them. This article will explain in detail how to use functions and methods in PHP to implement this function to help readers better understand and use it. Whether you are a beginner or an experienced developer, you can gain useful knowledge and practical tips from this article. Let’s get started!
Question content
I refer to some examples to successfully extract each part of user input. But it can only be withdrawn once. There should be 2 loops for extracting multiple inputs and saving the markers in an array. I'm stuck on an array, what should I do?
question: write a program that accepts string tokens in the format of token1:token2:token3:token4 , where : is used as delimiter to separate tokens. there should be two functions, ingest and appearance. ingest takes a string, and stores it in the collection. appearance takes a string as input . it returns a normalized value between 0 to 1, where the value represents the percentage of appearances of stored tokens which have input as the prefix. state the space and time complexity of your solution.
expected outcome:
ingest('mcdonal:uk:employeea') ingest('mcdonal:hk:employeea') ingest('mcdonal:hk:employeeb') ingest('mcdonal:hk:employeec') ingest('fastfood') appearance('mcdonal') # > 0.8 appearance('mcdonal:hk') # > 0.6
My code:
String input; // For user input Scanner sentense = new Scanner(System.in); input = sentense.nextLine(); String[] ingestWords = {}; // Use ':' to seperate input StringTokenizer st = new StringTokenizer(input, ":"); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); }
Solution
I suggest you solve your problem in two steps.
First, the ingestion part: you need to accept words entered by the user and store each word in an arraylist<string></string>
, not a fixed size array, since you don't know in advance what you are going to get How many inputs.
The sample code is as follows.
public static void main(string[] args) { string input; list<string> ingestwords = new arraylist<>(); scanner sentence = new scanner(system.in); while (sentence.hasnext()) { input = sentence.next(); if (input.equals("exit")) { // to stop receiving input break; } ingestwords.add(input); } sentence.close(); }
Second, the appearance part: given a string, you need to iterate the list of words from the ingestion part and check which words start with the given string.
For example, you can create a helper function like this.
void hasPrefix(String word, String str) { return word.startsWith(str); }
Applying this function to each word in ingestwords
will give you the number of words prefixed with str
. And you can figure out the percentage of occurrences from that.
The above is the detailed content of How to store string tokens into array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment