Creating yourn own string methods and properties in c sharp # without using any predefined or system properties and functions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mynamespace;
namespace Test_question
{
class Program
{
static void Main(string[] args)
{
string s, s2;
int i;
Console.WriteLine("ENter a string");
s = Console.ReadLine();
str ob = new str(s);
Console.WriteLine("Length of the string " + ob.len);
Console.WriteLine("Enter a position to extract character");
i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the character is " + ob.charAt(i));
Console.WriteLine("Enter string to concatenate");
s2 = Console.ReadLine();
Console.WriteLine(ob.concatenate(s, s2));
str ob2 = new str(s2);
Console.WriteLine("position is " + ob.indexof('a'));
Console.WriteLine("position is " + ob.lastindexof('u'));
Console.WriteLine(ob.substring(2));
Console.WriteLine(ob.substring(2, 4));
Console.ReadKey();
}
}
}
namespace Mynamespace
{
class str
{
string s;
int i = 0, j = 0, l = 0;
public str(string t)
{
s = t;
}
public int len
{
get
{
i = -1;
try
{
while (i >= -1)
{
i++;
char ch = s[i];
}
}
catch (Exception e)
{
if (i == 0)
return (-1);
return i;
}
return (i);
}
}
public char charAt(int index)
{
char ch = ' ';
try
{
ch = s[index];
}
catch (Exception e)
{
Console.WriteLine("index out of bouind");
}
return (ch);
}
public string concatenate(string s, string s2)
{
str ob = new str(s);
str ob2 = new str(s2);
int l = ob.len;
int l2 = ob2.len;
int l3 = 0, k = 0;
if (l2 > l)
l3 = l;
else
l3 = l2;
i = 0; j = 0;
string s3 = "";
try
{
while (k < l)
{
s3 += s[k++];
}
k = 0;
while (k < l2)
{
s3 += s2[k++];
}
}
catch
{
}
return s3;
}
public int indexof(char ch)
{
str ob = new str(s);
int l = ob.len;
for (i = 0; i < l; i++)
if (s[i] == ch)
return (i + 1);
return (-1);
}
public int lastindexof(char ch)
{
str ob = new str(s);
l = ob.len;
for (i = l - 1; i > 0; i--)
if (s[i] == ch)
return (i + 1);
return (-1);
}
public string substring(int x)
{
str ob = new str(s);
string s2 = "";
l = ob.len;
for (i = x; i < l; i++)
s2 += ob.charAt(i);
return s2;
}
public string substring(int x, int y)
{
str ob = new str(s);
string s2 = "";
l = ob.len;
for (i = x; i < y; i++)
s2 += ob.charAt(i);
return s2;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mynamespace;
namespace Test_question
{
class Program
{
static void Main(string[] args)
{
string s, s2;
int i;
Console.WriteLine("ENter a string");
s = Console.ReadLine();
str ob = new str(s);
Console.WriteLine("Length of the string " + ob.len);
Console.WriteLine("Enter a position to extract character");
i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the character is " + ob.charAt(i));
Console.WriteLine("Enter string to concatenate");
s2 = Console.ReadLine();
Console.WriteLine(ob.concatenate(s, s2));
str ob2 = new str(s2);
Console.WriteLine("position is " + ob.indexof('a'));
Console.WriteLine("position is " + ob.lastindexof('u'));
Console.WriteLine(ob.substring(2));
Console.WriteLine(ob.substring(2, 4));
Console.ReadKey();
}
}
}
namespace Mynamespace
{
class str
{
string s;
int i = 0, j = 0, l = 0;
public str(string t)
{
s = t;
}
public int len
{
get
{
i = -1;
try
{
while (i >= -1)
{
i++;
char ch = s[i];
}
}
catch (Exception e)
{
if (i == 0)
return (-1);
return i;
}
return (i);
}
}
public char charAt(int index)
{
char ch = ' ';
try
{
ch = s[index];
}
catch (Exception e)
{
Console.WriteLine("index out of bouind");
}
return (ch);
}
public string concatenate(string s, string s2)
{
str ob = new str(s);
str ob2 = new str(s2);
int l = ob.len;
int l2 = ob2.len;
int l3 = 0, k = 0;
if (l2 > l)
l3 = l;
else
l3 = l2;
i = 0; j = 0;
string s3 = "";
try
{
while (k < l)
{
s3 += s[k++];
}
k = 0;
while (k < l2)
{
s3 += s2[k++];
}
}
catch
{
}
return s3;
}
public int indexof(char ch)
{
str ob = new str(s);
int l = ob.len;
for (i = 0; i < l; i++)
if (s[i] == ch)
return (i + 1);
return (-1);
}
public int lastindexof(char ch)
{
str ob = new str(s);
l = ob.len;
for (i = l - 1; i > 0; i--)
if (s[i] == ch)
return (i + 1);
return (-1);
}
public string substring(int x)
{
str ob = new str(s);
string s2 = "";
l = ob.len;
for (i = x; i < l; i++)
s2 += ob.charAt(i);
return s2;
}
public string substring(int x, int y)
{
str ob = new str(s);
string s2 = "";
l = ob.len;
for (i = x; i < y; i++)
s2 += ob.charAt(i);
return s2;
}
}
}
Comments