Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:40:46+00:00 2026-06-08T13:40:46+00:00

I had an interview where I could use any language I wanted to in

  • 0

I had an interview where I could use any language I wanted to in order to calculate how many times each word appeared in a text file.

I solved it in C by reading in each word and adding it to a tree structure – If the word was all ready in the structure, the algorithm would add one to a counter I added in the node.

Well the person said I had to much code and it was messy, and said I could do it in PHP with about 3 lines of code.

I started to use the explode() to separate the string into a array of words, but was stuck after that.

Does anybody know how this could be done in PHP with a couple lines of code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:40:48+00:00Added an answer on June 8, 2026 at 1:40 pm

    An easy way to do this is to read the complete file in as a string with file_get_contents(), split it up on whitespace, and run the resulting array through array_count_values()

    $file = file_get_contents( 'text_file.txt');
    $array = preg_split( '/\s+/', $file); 
    $counts = array_count_values( $array);
    

    Done!

    However, this isn’t perfect, as punctuation can mess up your count. So, as Mark Baker points out, we can go back to my original method of getting all of the words in the file with str_word_count(), then running that array through array_count_values():

    $file = file_get_contents( 'text_file.txt');
    $words = str_word_count( $file, 1);
    $counts = array_count_values( $words);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday i had a question in an interview which i thought i could find
Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis)
I had an interview question asking this: text file has following lines> 1: A
this is acutally one of the interview questions I had. I didnt really use
I had an interview today which involved this very question and in order to
I had an interview and I could not answer this question. You have an
During a job interview I had some time ago I was asked to calculate
I had an interview days ago and was thrown a question like this. Q:
I had an interview and a question that seems really simple was not clicking
Today I had an interview for a developer job and I had to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.