@manhng

Welcome to my blog!

Get all words of a string

July 15, 2017 01:14

0) ToCharArray in JavaScript 

var s = "aabbbccccdc";
var arr = s.split('').join(',');

1) How to get all words of a string

https://stackoverflow.com/questions/4970538/how-to-get-all-words-of-a-string-in-c

 

static string[] GetWords(string input)
{
    MatchCollection matches = Regex.Matches(input, @"\b[\w']*\b");

    var words = from m in matches.Cast<Match>()
                where !string.IsNullOrEmpty(m.Value)
                select TrimSuffix(m.Value);

    return words.ToArray();
}

static string TrimSuffix(string word)
{
    int apostropheLocation = word.IndexOf('\'');
    if (apostropheLocation != -1)
    {
        word = word.Substring(0, apostropheLocation);
    }

    return word;
}

static void Main(string[] args)
{
string s = "aa bbb cccc d c";
string[] arr = GetWords(s);

foreach (var item in arr)
{
Console.WriteLine(item);
}
}

JavaScript Map & Reduce function

http://adripofjavascript.com/blog/drips/boiling-down-arrays-with-array-reduce.html

https://www.sitepoint.com/map-reduce-functional-javascript/

https://medium.freecodecamp.org/reduce-f47a7da511a9

 

var books = [

    { title: "Showings", author: "Julian of Norwich", checkouts: 45 },

    { title: "The Triads", author: "Gregory Palamas", checkouts: 32 },

    { title: "The Praktikos", author: "Evagrius Ponticus", checkouts: 29 }

];

 

// Get total of book checkouts

var total = books

    .map(function(b) {

        return b.checkouts; })

    .reduce(function(p, c) {

        return p + c; });

JavaScript Reduce functional

var arr = [{x:1},{x:2},{x:4}];

arr.reduce(function (a, b) {
  return {x: a.x + b.x}; // returns object with property x
})

// ES6
arr.reduce((a, b) => ({x: a.x + b.x}));

----------
const data = [
{a: 'happy', b: 'robin', c: ['blue','green']},
{a: 'tired', b: 'panther', c: ['green','black','orange','blue']},
{a: 'sad', b: 'goldfish', c: ['green','red']}
];

const colors = data.reduce((total, amount) => {
amount.c.forEach( color => {
total.push(color);
})
return total;
}, [])

//['blue','green','green','black','orange','blue','green','red']

JavaScript Map functional


var
animals = ["cat","dog","fish"]; var lengths = animals.map(function(animal) { return animal.length; }); console.log(lengths); //[3, 3, 4]

2.1) Get longest word of a string (JavaScript)

var str = "aabbbccccdc";
var obj = [...str].reduce((a, curr) => {
    a[curr] ? (a[curr] += curr) : (a[curr] = curr);
    return a;
}, {});
console.log(Object.values(obj));

2.2) Get longest word of a string (C#)

static void Main(string[] args)

{

    string s = "aabbbccccdc";
    var v = new List<string>();
    int len = s.Length;
    int start = 0;
    int end = 0;
    for (int i = 1; i < len; i++)
    {
        if (s[i] == s[start])
        {
            end++;
        }
        else
        {
            v.Add(s.Substring(start, end - start + 1));
            start = end = i;
        }
    }

    var retValue = v.OrderByDescending(m => m.Length).FirstOrDefault();
    Console.WriteLine(retValue);

}

2.2) Get longest word of a string (C#)


static void Main(string[] args)
{
    string s = "aabbbccccdc";
    Console.WriteLine("Input ~ Enter a string: {0}", s);

    // a a b b b c c c c d c
    // 0 1 2 3 4 5 6 7 8 9 10
    // 10 9 8 7 6 5 4 3 2 1 0

    // Step 1: arrInt = [9, 8, 4, 1]
    var arrInt = new List<int>();

    for (int i = s.Length - 1; i > 0; i--)
        if (s[i] != s[i - 1]) arrInt.Add(i - 1);

    // Step 2: arrInt = [1, 4, 8, 9]
        arrInt.Reverse();

    // Step 3: Insert " " at position of arrInt[i] + 1 + i
    for (int i = 0, n = arrInt.Count; i < n; i++)
        s = s.Insert((arrInt[i] + 1) + i, " ");

    // Step 4: Split string "aa bbb cccc d c" to array of string and return item that its max length
    var newArr = s.Split(' ');
    var retValue = newArr.OrderByDescending(m => m.Length).FirstOrDefault();

    foreach (var item in newArr)
        Console.WriteLine(item);

    Console.WriteLine("Output ~ The longest word: {0}", retValue);

}

Categories

Recent posts