004-007-007

Array Length and Search

Easy

Problem Description

array Length and Search

In this problem, you will create a program that reads a city name array from standard input, retrieves the number of elements using the length property, searches for specified city names using linear search, and displays the results to standard output.

Learning Objective: Understand how to get the number of elements using the length property and search for a specific value using linear search

Overview

Read city names from standard input, display the number of elements, perform linear search for two targets, and print the results.

Input Format

N
city1
city2
...
cityN
target1
target2
  • Line 1: number of cities N
  • Next N lines: city names
  • Next line: first search target
  • Next line: second search target

Specifications

  1. Read the number of cities N from standard input
  2. Read N city names and store them in a string array cities
  3. Read two search target strings
  4. Print the number of elements in the array
  5. Search for target1 using linear search; if found, print its index
  6. Search for target2 using linear search; if not found, print a message

Output Format

Number of cities: N
<target1> found at index: <index>
<target2> not found

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up