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.

Filter by
Sorted by
Tagged with
-1
votes
0answers
2 views

Why free() is generating an error when I use __strtok_r() in C. Is there any alternatives to __strtok_r()?

I am using the dynamically allocated string named *buffer to store the contents of the string time_digital, in order to tokenize ...
6
votes
2answers
57 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 ...
2
votes
1answer
40 views

LeetCode: Group Anagrams C#

LeetCode: Group Anagrams C# Given an array of strings, group anagrams together. ...
25
votes
10answers
68k views

Random string generation

I'm using this C# function to generate random coupons for a system. How can I improve it? ...
2
votes
1answer
109 views

A bitwise common prefix algorithm in Python

Is my algorithm slow because it has problems? Or, is any bitwise common prefix solution not going to give me the performance I'm looking for? After profiling my algorithm, I found that over 60% of ...
-2
votes
1answer
41 views

Get key and its value from a string “Key:Value;Key:Value;Key:Value” in C++ [on hold]

I provide a method as follow, How about your ideas? Would you share your ideas with me? Would you give me some recommendations in my code? live demo on coliru ...
-1
votes
2answers
33 views

Format and return a string from a function in C [on hold]

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 ...
0
votes
1answer
107 views

Reverse String in Java

I've been revamping on my coding puzzles practices. What is the complexity (both time and space) for this method? ...
3
votes
2answers
886 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 ...
2
votes
0answers
37 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 ...
3
votes
2answers
79 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" ...
7
votes
3answers
101 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 ...
0
votes
1answer
164 views

Lightweight read-only string implementation (C++)

In C++ currently we have few possibilities to work with strings: 1) by using plain old C-strings -- unfortunately, we have penalty for getting length of string each time, and we should manually ...
3
votes
2answers
44 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 ...
0
votes
1answer
46 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
49 views

Check for pangrams in elisp

I'm trying to find pangrams ie sentences that contain all the letters of the alphabet, for instance "The quick brown fox jumps over the lazy dog". I'm an absolute beginner in elisp, and it ended up ...
1
vote
4answers
107 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 ...
9
votes
5answers
11k views

Convert a custom object to a query string

I wrote an extension method that takes a custom object and converts its properties to a query string for use in a URL. You can specify what properties you want included by using a ...
7
votes
2answers
6k views

Balance braces method

This is a continuation of this question, at least the first part of that question. I want to check if everything in the new code is right or if there are other way to make it better. Exercise The <...
3
votes
1answer
61 views

Random password generator with choice of three strengths

My code works fine but I definitely have unecessary repeat code. I'd like to use functions so I don't have repeat code. I would appreciate any advise on how to make my password generator code more ...
4
votes
1answer
81 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 ...
3
votes
2answers
198 views

Counting the number of ways to decode a string

I am working on problem where I need to decode a string: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... ...
2
votes
0answers
27 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 ...
6
votes
1answer
337 views

Slices of Strings

In an attempt to summarize the question and all the good answers to this question, I decided to try to write a pair of string extensions that could split a string in respect to number of slices ...
4
votes
1answer
28 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
42 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 ...
9
votes
5answers
4k views

Funny String Java Solution

I'm a little rusty in Java, but I'm using it to get ready for interviews. Here's the problem description from HackerRank: Problem Statement Suppose you have a string \$S\$ which has length \$...
5
votes
2answers
624 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:...
4
votes
2answers
2k views

Using C++ to check if string s is subsequence of string t

The problem I want to solve is very simple: Given a string s and a string t, check if s is subsequence of t. there is only lower case English letters in both s and t. t is potentially a very long (...
3
votes
1answer
124 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 ...
5
votes
1answer
577 views

String algorithms and locale

I am trying to write some string algorithms which I want to work for any kinds of strings and/or locale. I managed to get some results that do work, but I am not sure that what I am doing is idiomatic ...
1
vote
1answer
40 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() ...
1
vote
2answers
164 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
57 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 ...
7
votes
4answers
3k views

Find palindromic substrings as efficiently as possible

I've written a function to calculate all the distinct substrings of a given string, that are palindromes. The string is passed as the argument to the function. For example, the string abba is a ...
7
votes
3answers
15k views

Splitting URL query string to key-value pairs

I have a URL query string "key={value}&key2={value2}&key3={value3}" and have to produce a key-value pair of the string by splitting ...
4
votes
1answer
530 views

String similarity algorithm (c++)

Parameters: first - the first string. second - the second string. similarCharacters - ...
227
votes
14answers
37k views

Calculating entropy of a string

We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality. I whipped up this simple method which counts unique characters in a string, but it is quite ...
7
votes
3answers
4k views

Converting a boolean list to string, and vice versa

I am currently using this to convert a List of booleans to a string ...
4
votes
2answers
767 views

Streamline code for checking if a directory exists and saving a file

I hate this code. What is the slickest way to write the following: ...
2
votes
1answer
35 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' ...
2
votes
2answers
125 views

Python program to get the first word

Given a string the task it to find its first word with some rules: The string can have points and commas A word can start with a letter, a point or space One word can contain one apostrophe and it ...
4
votes
1answer
49 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
69 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 ...
34
votes
4answers
8k views

A lightning-fast StringBuilder

In the process of trying to build a serializable data structure, I found myself building large strings, which gets very slow because VBA copies a string every time concatenation is performed. To ...
1
vote
3answers
33 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: ...
2
votes
1answer
57 views

Concatenating an already existing filename to create a new file

I had answered this question from stackoverlow and the code below is code that I wrote to explain to the OP a possible way to achieve what I believe they were looking for. The general idea that I ...
5
votes
4answers
236 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, ...
4
votes
1answer
85 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
374 views

Maximum character insertion

I would like to know any suggestions about to improve my code and/or get a better approach for this problem solution and your rating of my approach to the problem.(besides removing the ...