- Use a loop to force the user to enter valid integer in the range 2 to 5 inclusive, and handle all exceptions.
- The program will then input the strings into the array.
- Find and display the shortest string stored in the array.
- Display the strings that are stored in the array in reverse order,
- As well, display string length for each string as shown below.
How many strings do you wish to enter: 3
Enter string #1: Cats and rats
Enter string #2: just rats
Enter string #3: rats
The shortest strings is "rats"
rats 4
just rats 9
Cats and rats 13
VulpesPosted Apr 21, 2011, 5:34 AM
So, I've just wasted 15 minutes answering it again :(
Soft CornerPosted Apr 21, 2011, 1:34 AM
Try this,
class Program
{
static void Main(string[] args)
{
try
{
int numberofStr;
string[] str;
Console.WriteLine("How many strings do you wish to enter:");
numberofStr = Convert.ToInt32(Console.ReadLine());
if (numberofStr >= 2 && numberofStr <= 5)
{
str = new string[numberofStr];
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine("Enter string #"+i+":");
str[i] = Convert.ToString(Console.ReadLine());
}
for (int j = 0; j < str.Length; j++)
{
Console.WriteLine(str[j].ToString() + " " + str[j].Length);
}
Program pg = new Program();
string min = pg.MinStr(str);
Console.WriteLine("shortest string :"+ min);
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public string MinStr(string[] numbers)
{
string m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
if (m.Length > numbers[i].Length)
m = numbers[i];
return m;
}
}
Ankit NandekarPosted Apr 21, 2011, 1:13 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
public string Reverse(string str)
{
int len = str.Length;
char[] arr = new char[len];
for (int i = 0; i < len; i++)
{
arr[i] = str[len - 1 - i];
}
return new string(arr);
}
static void Main(string[] args)
{
string s="";
Program obj = new Program();
string[] arr1 = {"Cats and rats", "just rats", "rats"};
s = arr1[0];
/*Code For Reverse a String in Array*/
Console.WriteLine("Reverse String is:"+obj.Reverse("ankit"));//For Reverse
/*Code For Find the Length of Strings*/
foreach (string str in arr1)
{
Console.WriteLine("{0} Length:{1}", str, str.Length);
}
/*Code for Find Shortest string in Array*/
for (int i = 0; i < arr1.Length; i++)
{
if (s.Length > arr1[i].Length)
{
s = arr1[i];
}
}
Console.WriteLine("shortest string is:"+s);
Console.Read();
}
}
}
I m attaching code with this u can try .
If this is useful information for you then please mark as accepted answer
FroglegPosted Apr 21, 2011, 1:05 AM
{
string[] str = new string[] { "Cats and rats", "just rats", "rats" };
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (string s in str)
{
listBox1.Items.Add(s);
}
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
for (int i = str.Length - 1; i >= 0; i--)
{
listBox1.Items.Add(str[i]);
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
var getShortest = str.OrderBy(name => name.Length);
string shortestName = getShortest.First();
listBox1.Items.Add(shortestName);
}
}