1

Here is my prompt:

  1. Retrieve a JSON file from a remote URL. Your solutions should pull this from a settings file (app.config, web.config. etc). (I have the url)

  2. Determine if a provided string is a palindrome. Alphanumeric chars will be considered when evaluating whether or not the string is a palindrome.

  3. Parse the retrieved JSON file, and pass each element in the "strings" array, into the function in step #2. You should print out the string and result.

I am new to C# and I am having trouble figuring out how to read the json file from the url, and then using it for the function. I am pretty much stuck on how to start this. Any tips?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace ConsoleApplication1
{
class Program
{

    public static bool IsPalindrome(string value)
    {
        int min = 0;
        int max = value.Length - 1;
    //    while (true)
       {
            if (min > max)
            {
                return true;
           }
           char a = value[min];
            char b = value[max];
            if (char.ToLower(a) != char.ToLower(b))
            {
                return false;
            }
            min++;
            max--;
        }
    }

    static void Main() {
        using (WebClient webClient = new System.Net.WebClient())
        {
            WebClient n = new WebClient();
            var json = n.DownloadString("URL");
            string valueOriginal = Convert.ToString(json);
            //Console.WriteLine(json);
        }

        string[] array = {

          };

        foreach (string value in array)
        {
            Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
        }
    }
}
}

Sample JSON:

{
  "strings": [
    {
      "str": "mom",
      "result": "true"
    },
    {
      "str": "Taco Cat",
      "result": "true"
    },
    {
      "str": "university",
      "result": "false"
    },
    {
      "str": "Amore, Roma.",
      "result": "true"
    },
    {
      "str": "King are you glad you are king",
      "result": "false"
    }
  ]
}
3

1 Answer 1

1

Here is how you can get a json string from a URL:How to get a json string from url?

Here is how you can deserialize a json: Deserialize an Object

Here is how you can write a function to check if a string is palindrome:Check if a string is a palindrome

You are on the right track. Below I have updated your code to get what you need:

    class Program
{
    private static void Main(string[] args)
    { 
        // Get JSON from URL
        var json = GetJasonFromUrl(Properties.Settings.Default.url);

        // De-serialize JSON into a list
        var deserlizedJson = DeserializeMyJson(json);

        // Go through each item in the list and determine if palindrome or not
        foreach (var item in deserlizedJson)
        {
            if (IsPalindrome(item.Str))
                Console.WriteLine(item.Str + " is palindrome");
            else
                Console.WriteLine(item.Str + " is not palindrome");
        }
    }

    private static string GetJasonFromUrl(string url)
    {
        string result;

        try
        {
            using (var webClient = new WebClient())
            {
                result = webClient.DownloadString(url);
            }
        }
        catch (Exception)
        {
            result = string.Empty;
        }

        return result;
    }

    private static IEnumerable<Palindromes> DeserializeMyJson(string json)
    {
        return JsonConvert.DeserializeObject<IEnumerable<Palindromes>>(json);
    }

    // Assuming your function is tested and correct
    private static bool IsPalindrome(string value)
    {
        var min = 0;
        var max = value.Length - 1;

        while (true)
        {
            if (min > max)
                return true;
            var a = value[min];
            var b = value[max];

            if (char.ToLower(a) != char.ToLower(b))
                return false;

            min++;
            max--;
        }
    }
}

internal class Palindromes
{
    public string Str { get; set; } = string.Empty;
    public bool Result { get; set; } = false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get an error with 'Properties' when getting the json from the url
I assumed you are placing the URL in the application settings of your project. You can replace it with a hard-coded value for now until you decide where to place it so that in future you can change it without having to touch the code. Settings is under Properties of the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.