Questions tagged [strings]

A string is a sequence of characters. It is commonly used to represent text or a sequence of bytes. Use this tag along with the appropriate programming language being used.

2
votes
1answer
211 views

Random string generation in Java

Can random string generation be faster in Java? Here is my current code ...
2
votes
1answer
135 views

Trimming email address to hide the name

I am looking to hide an email id name, by only showing first 4 letters of the email. The code works fine. Looking for opinion if I could make it better, more readable and maybe even more efficient if ...
2
votes
4answers
100 views

K&R Exercise 1-19. Reverse program input one line at a time

Intro I'm going through the K&R book (2nd edition, ANSI C ver.) and want to get the most from it: learn (outdated) C and practice problem-solving at the same time. For that reason, I'm trying to ...
4
votes
3answers
1k views

Extracting a specific substring in C

I have a string like this: char buffer[] = "blablabla$GPTXT->SOME CODES HERE<-\r\n$GPRMCblablabla"; It is sent from an external device and changes every 1 ...
5
votes
1answer
62 views

Check if we can form a word across words in a sentence

I've come across a simple challenge: given a phrase, check if we can form the given word vertically by stacking words in rows. For example, we can form the word ...
0
votes
1answer
256 views

Rabin-Karp String matching

I am learning algorithms and have implemented the Rabin-Karp string search algorithm. I didn't implement a rolling HashCode, but it's judged correct by this ...
1
vote
2answers
100 views

Find if cookie name exists in the cookie string

I need to determine whether a cookie name is available or not in the cookie string. I have achieved this. We can use cookie-parser package but I don't want to use that package so I have written the ...
4
votes
1answer
152 views

Convert boolean list to list of base10 integers

Context: I'm coding up a simple genetic optimization algorithm from first principles, and have decided to use boolean/binary strings as genes so that I can flip bits on or off as a form of mutation. ...
1
vote
3answers
81 views

Printing a table of letter frequencies using Python

What is the recommended (standard, cleaner, more elegant, etc.) method in Python to get a string with all the alphabet letters? Here's three versions of code to generate a dictionary with keys as ...
9
votes
4answers
1k views

Program that finds the longest word not containing one of the disallowed characters

Problem definition Find the longest word that doesn't contain any of the following characters: gkmqvwxz Input comes from a file named ...
-2
votes
1answer
58 views

Remove key-value pairs from string when only key is given

I have a need to parse strings that contain pairs of key : value, and remove a specific pair, when I'm only given the key. Example: Given the string ...
0
votes
1answer
43 views

CodingBat warm up: conditionally prepending “not” to a string

I solved this CodingBat problem: Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: ...
0
votes
1answer
55 views

Formatting list of queries in python

I have a file of SQL (actually HiveQL) queries terminated by semicolon (;). I need to obtain ...
4
votes
1answer
39 views

Find labels in string in Haskell

I wrote code that parses a table header string in order to obtain two string tags, one for variable name (like GDP) and another for unit of measurement (like ...
1
vote
2answers
100 views

Converting a String to USD

I'm working on a clientside iOS app for my school that involves students purchasing things, and I wrote a String extension for formatting a ...
2
votes
2answers
57 views

Method that receives a hexadecimal string and returns a formatted matrix of those values

This method is part of a larger project and comes after encoding ascii characters into hexadecimal values, so it assumes the parameter is always a valid hex string. ...
4
votes
2answers
93 views

C++ string tokenizing without streams, with certain conditions

My concern is mainly the correctness of the algorithm for tokenizing purposes. The code compiles as it is called as a member function in my program. The code does pass my small amount of unit tests ...
7
votes
3answers
553 views

Count number of gemstones in a set of minerals

This is the "Gemstones" problem on Hackerrank. John has collected various rocks. Each rock has various minerals embeded in it. Each type of mineral is designated by a lowercase letter in the range ...
12
votes
2answers
3k views

Make a beautiful binary string

This is the "Beautiful Binary String" problem at HackerRank: Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring 010. In one step,...
1
vote
2answers
1k views

Check if a string is a valid english word, and convert to a valid english word

This is an interview question. For the purpose of the question, a valid English word is a string that begins with an uppercase English letter, and the rest of the string contains only lowercase ...
1
vote
3answers
108 views

Checking if a string is funny or not

The idea is this: To determine whether a string is funny, create a copy of the string in reverse e.g. . Iterating through each string, compare the absolute difference in the ascii values of the ...
4
votes
2answers
117 views

Templated string formatting

This function formats a string by replacing every occurrence of '{n}' with the corresponding argument that was passed to the function where n is an index that ...
3
votes
2answers
630 views

Swapping pairs of characters in a String

Question: Write a program to swap pairs of characters of a String. In case of odd number of characters last character should remain unchanged. Examples: Input: HI Output: IH ...
5
votes
4answers
770 views

Take a desired string, iterate through objects to see if it exists in a given field and append a number until a unique string is found

I wanted to in the most generic way possible write a method in C# to achieve the following. Take in a string, a set of objects, and a function to access the field of a given object Look at all of the ...
10
votes
2answers
131 views

Prettifier isn't pretty

Some context I'm implementing a self-closing pairs auto-completion feature in Rubberduck. One of the challenges is that the VBIDE API doesn't allow editing a line of code by "injecting" characters: ...
2
votes
1answer
213 views

Safe string formatting in Python

I am a fan of f"{strings}" in Python, However some situations could possibly be dangerous when doing f strings from user input, leaking API keys or even code ...
3
votes
4answers
2k views

Find longest Palindrome substring

I am preparing for technical interview and I found this question on geeksforgeeks, but I did not understand their solution. So, I have written my own solution. I want to optimize this code. ...
0
votes
1answer
146 views

Parse JSON strings as tables using Python

Needed a simple CSV file that had headers and no nesting of data to consume in Tableau. The JSON file I was consuming had nested dictionaries and lists (which sometimes had in themselves nested lists/...
4
votes
2answers
497 views

Longest word in dictionary that is a subsequence of a given string

I am trying out the following former Google interview question. The Challenge Given a string S and a set of words D, find the longest word in D that is a subsequence of S. Word W is a ...
5
votes
2answers
137 views

Rust Echo Command Implementation

I'm just starting out in Rust and I find the concept of ownership confusing so I wrote an implementation of the echo command. I would like to know if I could have ...
3
votes
3answers
673 views

Morgan and a String HackerRank challenge

I am solving a HackerRank problem called 'Morgan and a String'. Given two strings, each consisting of up to 105 uppercase characters, the task is to form the lexicographically smallest string from ...
0
votes
1answer
43 views

Prevent multiple ifelse statements in php [closed]

I'm trying to figure out a way to make my code more readable and maintainable. While rewriting some ugly spaghetti code, I stumbled across this: ...
2
votes
1answer
180 views

Split a string based on a delimiter in C

Basically, this code splits a string into substrings based on a token. The printf call is just a placeholder for something useful for each substring and can be ...
1
vote
2answers
60 views

Output string length and classical Fibonacci sequence in a web page

I wrote a JavaScript and XHTML webpage. I think the format of the output is not better or the validation process is not as it is supposed to be. How can I improve the code? How can I make it more ...
0
votes
1answer
54 views

Formatting fractions found in a given string to html fraction

I'm working on a site running woocommerce and wp where the user will add/has added a dimension attribute. The dimension string will take the following format originally: Ht: 30 1⁄2" W: 22 1⁄2" D: ...
3
votes
0answers
72 views

Count number of substrings which contains particular strings

I need to count the number of substrings of s which contains both the strings a and b. This ...
3
votes
2answers
330 views

Converting an input string to a list of sub lists with numbers

I am learning Python 3. One thing I am doing to get some repetition is going through checkio. It is a website with coding exercises. One of the exercises gives an input string as follows: ...
2
votes
2answers
105 views

Find a palindromic string in C

This program tests whether a string is a palindrome or not. Can there be any improvements or shortcuts that can be used in the program. You can give any type of string as input. The program finds does ...
6
votes
2answers
215 views

Random quote rotator

On my website, I created like this banner that rotates quotes from an array I made. ...
4
votes
1answer
775 views

Convert string of hex into vector of bytes

I want to write a function that gets a string of hex numbers (two hex numbers represent a u8 value) then returns a vector of u8 ...
0
votes
1answer
22 views

Embolden parts of auto-complete suggestions that don't match the query

I made a function to embolden text that doesn't match a string, but it appears to be very performance-heavy. I am certain that there are much better ways to do this, but with what I tried, I ran into ...
4
votes
1answer
554 views

Multiple client TCP stream reader

My server code is currently reading String data over a TCP connection form 2 different client computers. To do this I am duplicating the ...
3
votes
2answers
54 views

Utility functions for processing tweets or texts

I have 4 functions that process texts or tweepy.models.Status object (this object stores text information, author information, retweets & likes information of a ...
6
votes
2answers
1k views

Looping through a string finding nested brackets

So I've been trying to check through a string, looping for, [ (start) and ] (end). In this, I also have to check for nested ...
0
votes
1answer
203 views

Shuffle a string in a repeatable way

The following function is designed to accept a string of characters and an integer. The function returns a string that's a reordered version of the original string. The reordering process is ...
5
votes
3answers
905 views

Function for creating unique strings for urls

I wrote this function in order to create unique url's for my site. Using these urls, visitors could demo site functionality without having to create a login. The function was intended to return a ...
4
votes
3answers
62 views

Replace $number with $number+1 in std::string

I want to detect $number substring in std::string and then replace it with $number + 1. For ...
3
votes
1answer
98 views

Search and Replace (FreeCodeCamp intermediate algorithm scripting)

I completed the challenge and passed the tests: Perform a search and replace on the sentence using the arguments provided and return the new sentence. First argument is the sentence to perform ...
3
votes
1answer
127 views

Bool function in PHP to check if you run the script inside docker

I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After some research I found a solution that people check some processes via ...
4
votes
5answers
182 views

Use filename to determine file type

This is legacy, so I'm not really looking for improvements on architectural approach. Here's what the method does: Extracts filename from a string containing full file path (passed in as parameter) ...