Question

write a function which takes a string argument and a character argument. It should return a...

write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the end of input (we'll call that EOF from now on). It should pass these lines to the function you wrote. If the character is in the line, it should print "FOUND: " and then the full line. If the character is not in the line, it should print "NOT FOUND: " and then the line. Assume lines of input are no longer than 1000 bytes in length. Also, make sure that your program can accept an arbitrary number of lines of input. Either 5, 10, 90, or 1000 lines of input can be given after the first character and it should still work as intended even if you gave the program more input. Again, signal the EOF with CTRL+D. You should only need one buffer

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.IOException;

import java.util.Scanner;

public class Driver{

public static void main(String args[]) throws IOException{

System.out.println("Enter the char ");

char c = (char) System.in.read();

Scanner in = new Scanner(System.in);

in.nextLine();

System.out.println("Enter a String");

String str = in.nextLine();

if(isFound(str, c) == 1)

System.out.println("FOUND");

else

System.out.println("NOT FOUND");

}

private static int isFound(String str, char c) {

for(int i = 0; i < str.length(); i++)

if(str.charAt(i) == c)

return 1;

return 0;

}

}

R Markers|? Properties!?Serversla Snippets , Console × | @ Javadoc terminated> Driver (3) [Java Application] C:Program FilesJavaljre1.8.0_40bin javaw.exe (13-Feb-2018 6:37:32 arm Enter the char Enter a String put can be given after the first character and it should still work as intended even if you gave the program more input. Again, signal the EOF with CTRL+D OUND

**Comment for any further queries.

Add a comment
Know the answer?
Add Answer to:
write a function which takes a string argument and a character argument. It should return a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Write a function that takes two arguments, both strings. Print "YES" if the second string argument...

    Write a function that takes two arguments, both strings. Print "YES" if the second string argument contains only characters found in the first string argument. Print "NO" if the second string argument contains characters not in the first. Here are some sample calls. In your program, call the function 3 times with the same arguments shown below. makeStr("hello", "") makeStr("hello", "olleloheloeloheloel") makeStr("hello", "olleloheloteloheloel") # YES # YES # NO

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • Write a program with a function that accepts a string as an argument and returns a...

    Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

  • Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes...

    Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes a string argument from the command line and outputs the string in reversed order You have two constraints: Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character t...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the...

    Write a function named mostFrequent that takes two parameters: 1. inFile, a string that is the name of an input file 2. outFile, a string that is the name of an output file The input file inFile exists when mostFrequent is called; mostFrequent must create outFile. The input file contains only lower case letters and white space. The function mostFrequent identifies the letter(s) that appear most frequently on each line of inFile and writes them to a corresponding line of...

  • Write a function that accepts a pointer to a C-string as its argument. The function should...

    Write a function that accepts a pointer to a C-string as its argument. The function should count the number of times the character 'w’ occurs in the argument and return that number.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT