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,670
questions
2
votes
3answers
1k views
String whitespaces
Here is a shortest possible syntax for replacing whitespaces with null and throwing an exception where it is not allowed:
...
3
votes
1answer
131 views
Removing duplicate Unicode characters
Motivation
I came across an interesting question on SO: determine-if-string-has-all-unique-characters and thought about providing an extension method to enable duplicate removal given some kind of ...
12
votes
4answers
2k views
Comma Code - Automate the Boring Stuff with Python
Below is my code for the Comma Code problem from Chapter 4 of Automate the Boring Stuff with Python. Is there anything I can do to make it cleaner?
Comma Code
Say you have a list value like this:
...
6
votes
4answers
208 views
A C# struct representing a string that can be neither null nor white space
I've written a C# struct in order to encapsulate the idea of a string being neither null nor ...
3
votes
1answer
97 views
LeetCode Reverse Words in a String without using any java library
The following problem is from LeetCode:
Given a string, you need to reverse the order of characters in each
word within a sentence while still preserving whitespace and initial
word order.
...
7
votes
1answer
254 views
Replacing spaces with tabs. Exercise 1-21 in “The C Programming Language”
I'm looking for feedback on the code I came up with for exercise 1-21 in "The C Programming Language" by K&R.
Write a program entab that replaces strings of blanks with the minimum number of ...
1
vote
0answers
37 views
Functions to obfuscate and deobfuscate String. Am I missing out any edge cases?
I have 2 functions, which are going to hide/ unhide the multiple languages plain text.
Security is not a major concern.
The objectives are
Just want to find a way to hide multiple ...
3
votes
1answer
980 views
Validate URL with Regex and java.net.URL
I'm writing a URL validator. Firstly, it checks for special characters in the input. Secondly, it adds 'http://' and checks for validity.
...
2
votes
1answer
56 views
Function to convert “path” into regular expression
This function converts a string like:
/{?lang(2-5):lowercase}/{page(2+):lowercase}/{article}-{id:integer}*.html*
Into the following regular expression:
...
4
votes
2answers
73 views
Replace part of the string with n size with string of m size
I wanted to understand memory and pointers better before I endeavour into file path manipulations for larger project. This is simple replace part of the string test function that I need to rewrite ...
3
votes
1answer
176 views
Extract numbers from the head of a string in BASH script
In my current BASH script project I am processing a string, which cannot have spaces.
At one point I get to where it is possible to have a series of digits. The series can be of any length, including ...
8
votes
4answers
1k views
Read string of any length in C
I've written code in C for reading a string of any length, and printing the string. It's one of my first C programs. I posted the program as part of a Stack Overflow question, and received a comment:
...
5
votes
4answers
1k views
Confirm the ending of a string
Challenge:
Check if a string (first argument, str) ends with the given target
string (second argument, target).
This ...
9
votes
3answers
1k views
Insert str into larger str in the most pythonic way
I want to insert a small str into a larger one in the most pythonic way.
Maybe I missed a useful str method or function ? The function that insert the small str into the larger one is ...
5
votes
1answer
620 views
Convert a hex string to base64
I wrote a program that converts a hex-encoded string to base64. It's my solution to the first of the Cryptopals challenges.
My main concerns are:
Portability. I don't want to rely on implementation-...
7
votes
2answers
116 views
K&R2 solution to exercise 1-18 (discarding trailing whitespace and empty lines)
Recently I have started learning C++ and I decided to learn C along the way to improve my understanding of how computers work. For C I am using "The C Programming Language Book" by Brian Kernighan and ...
9
votes
5answers
234 views
Stream text scanning
I've written some utilities to read text from streams. One of these, the ReadTo method, is to read text from a specified stream until a given text is encountered, ...
2
votes
3answers
98 views
Mapping words to letters which contains
Given sentence for example: ala ma kota, kot koduje w Javie kota I should to every letter occurring in the sentence I should map every string that this letter ...
6
votes
2answers
196 views
1
vote
0answers
131 views
Fast text search in Rust
I have a large file that I want to search for text as fast as possible. There is not a memory contraint, so I am loading the entire file into memory so that I can use the rayon parallelism crate in ...
6
votes
2answers
685 views
Writing 1.5 million rows in a file
I am generating SQL statements (of MySQL format) and writing into a file to create 10,000 tables. Each table contains 5 columns with 1.5 million rows of data in each column. I generated these data by ...
2
votes
0answers
53 views
Find text between two strings in data [closed]
I download a page to get it's source code. I then extract the first segment of the page that is between two strings that appear in the source code more than once.
How can my code be improved?
...
0
votes
2answers
100 views
Random string generator that 'guesses' user inputted word [closed]
This takes a user inputted string, then tries to randomly 'guess' it. The problem is that whenever the input's length is 6 digits or more, it takes an incredibly long time to process it. Is there a ...
4
votes
2answers
120 views
Converting a set into a string
The input is a set of alphabets.
The output is a string of 32 in length.
For any letter in the set if it is preceded by a '¬' we replace the n-th character in our output string with '0', such that n ...
6
votes
3answers
837 views
LeetCode: Group Anagrams C#
LeetCode: Group Anagrams C#
Given an array of strings, group anagrams together.
...
7
votes
2answers
119 views
Pythonic Substring Anagram Index
I'm working on improving my Python skills and have been doing a daily code problem. For the problem:
Given a word W and a string S, find all starting indices in S which are anagrams of W
For example ...
-1
votes
2answers
250 views
Format and return a string from a function in C [closed]
Write a function that will format a string and return it
A fixed length of string is malloc-ed. snprintf is used to format the string and then the buffer is ...
7
votes
3answers
214 views
String routines
These are a few string routines (just the mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good ...
3
votes
2answers
92 views
Reverse chars of each word in a string
An algorithm to reverse characters of each word in a sentence.
What would be your concerns regarding the following solution?
Example input: "I love New York"
output: "I evol weN kroY"
...
0
votes
1answer
52 views
String manipulation in Python
I have this very ugly piece of code which runs ethtool and then parses the output (Lilex is just a static class encapsulating subprocess and is not relevant to the ...
3
votes
2answers
72 views
JavaScript number obfuscater
I got inspired to do this after seeing this repo containing a bunch of weird syntactical quirks in JS, namely this example called "It's a fail!". The example was essentially the following:
You ...
1
vote
4answers
2k views
“String” contains function in C
This function checks whether or not a substring needle exists in another string haystack and returns the position if it does or ...
3
votes
2answers
927 views
Can this code, to convert string to integer, be made more compact?
Problem:
String to integer(leetcode)
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary ...
2
votes
0answers
33 views
Command String Parser
I'm working on a library that, in essence, splits a string at whitespace, with extra rules:
Consecutive whitespace is collapsed, and trimmed at the start and end.
Whitespace wrapped in quotes is not ...
4
votes
1answer
102 views
Create HTML structure dynamically
We had a long HTML string with information about the makers of our software. However, for i18n this was unwieldy. So I started to refactor, so only each person's description is translateable, not all ...
2
votes
0answers
389 views
Rosalind - Finding a Shared Motif
Here is my attempt to implement the Ukkonen algorithm for the Finding a Shared Motif problem on rosalind.info.
The code works, it produces a correct answer. It does so in 11 seconds. I am hoping to ...
6
votes
1answer
45 views
Search for strings in a list that have > 90% similarity
Explanation:
I am working on String comparison in which I want to cross-compare the list of the same input string.
For the same string, I am continuing the loop but if a single string will have ...
3
votes
1answer
492 views
Lightweight read-only string implementation (C++)
In C++, currently we have few possibilities to work with strings:
by using plain old C-strings - unfortunately, we have penalty for getting length of string each time, and we should manually control ...
3
votes
1answer
66 views
Determine fragment identifier from HTML page in Swift
For an iOS app which helps me rolling back vandalism on Stack Exchange, I have a piece of Swift code which downloads a revision page (example) and tries to find the 'spacer' fragment just above a ...
5
votes
2answers
716 views
Reducing the time for rolling hash
I tried this problem below and solved it in O(test_cases * string_length * pattern_length) complexity involving three nested loops.I want to know how can i further decrease time. I figured as much:
1:...
1
vote
1answer
53 views
Short function to remove unnecessary whitespace
I have a function consisting of one line of code:
def trimString(string):
""" Remove unnecessary whitespace. """
return re.sub('\s+', ' ', string).strip()
...
3
votes
1answer
142 views
String.IndexOfAny(string, string[]) (lowest position for group of needles in haystack)
This is a follow-up to this question with bug fixes, question and code improvements from @dfhwze, @PieterWitvoet, @HenrikHansen, @t3chb0t. I am still hoping for an improved approach or algorithm ...
2
votes
2answers
579 views
Best C# implementation of IndexOfAny(string, params string[])
Follow-Up question
.Net provides String.IndexOfAny(string, char[]) but not String.IndexOfAny(string, string[]).
The existing ...
2
votes
1answer
323 views
CodingBat: without_end
Link To Question
Given a string, return a version without the first and last char, so
"Hello" yields "ell". The string length will be at least 2.
without_end('Hello') → 'ell'
...
4
votes
1answer
53 views
Porting stemming algorithm from Java to Python
I have ported Stempel stemmer in Java (Apache Lucene) to Python. I come from Java world, so I'm afraid my translation might not be "pythonic" enough.
I would like to hear your feedback on quite ...
4
votes
2answers
512 views
Letter case permutation
The question can be found here https://leetcode.com/problems/letter-case-permutation
Given a string S, we can transform every letter individually to be
lowercase or uppercase to create another ...
1
vote
3answers
39 views
`set_includes` (string subsequence-containment) in Python 2
I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function:
...
4
votes
1answer
186 views
Random password generator (Java)
I wrote this program recently to generate random passwords for myself. It works well but it's been a long time since I wrote any java and am just looking on ways to clean up the code. I'm also not ...
5
votes
4answers
438 views
Reverse the word order in string, without reversing the words
I was tasked with using C++ to turn this
"Hello Jarryd, do you like socks?"
into:
"socks? like you do Jarryd, Hello";
Edit: The condition was to reverse this in place.
Here's what I came up with, ...
2
votes
1answer
218 views
Remediation for security vulnerability due to storing sensitive information in strings
Security assessment of our codebase reported the following vulnerability -
Vulnerability Description
The method x() in xyz.java stores sensitive data in a String object,
making it ...