JAVA PROGRAMMING
File Name
You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.
Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx".
You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 11. For example, if you delete the character in the position 22 from the string "exxxii", then the resulting string is "exxii".
Input
The first line contains integer nn (3≤n≤100) — the length of the file name.
The second line contains a string of length nn consisting of lowercase Latin letters only — the file name.
Output
Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0.
Examples
input
Copy
6 xxxiii
output
Copy
1
input
Copy
5 xxoxx
output
Copy
0
input
Copy
10 xxxxxxxxxx
output
Copy
8
The answer of the above question is as follow:
package removecharacter;
import java.util.Scanner;
/**
*
* @author AmitShaw
*/
public class RemoveCharacter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i,delCharacter = 0;
/*Enter the length of the character */
int lengthOfString = scan.nextInt();
scan.nextLine(); // to remove the redundant line
/*Enter the name of the file */
String str = scan.nextLine();
/*Wherever sub-string "xxx" is encountered a deletion has to be
performed */
for(i = 0;i < lengthOfString - 2;i++){
if(str.charAt(i) == 'x' && str.charAt(i+1) == 'x'
&& str.charAt(i+2) == 'x')
delCharacter++;
}
/*Print the appropriate Result */
System.out.println(delCharacter);
}
}
Thanks for liking my post.
For any query related to problem, please comment !!!
JAVA PROGRAMMING File Name You can not just take the file and send it. When Polycarp...
using java create hash set that can for the file use a txt file: Hi my name is rick. (a) Read one word from the file. (b) Remove all non-alphanumeric characters from the word. A non-alphanumeric character is any character other than the lowercase and uppercase English letters, and the numerals 0 through 9. (c) Add the modified word to the hash set.
can you use the isspace() method in the code what do i add to the code if i want to make sure that extra spaces dont affect the output of the code elect sumatra medium roast VS sumatra medium roast Det dat HETTI CV Surface Providesearch coffee record description to search for at that it was not found in the file the first on has another space in it Table 8-1 Some string testing methods Method Description isalnum() Returns true...
PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...
Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of: the number of characters in...
(c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...
language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...
TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input...
Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...
C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...
Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...