10 Replies - 1741 Views - Last Post: 24 October 2008 - 07:52 AM Rate Topic: -----

#1 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

string manipulation

Post icon  Posted 21 October 2008 - 08:10 AM

hi.. there is 2 fucntion encoding and decoding of 2 strings... in nencoding i have to put one string in another at random places... and in decoding i have to get the original string..

decoding is done.. but have some problem in encoding.. plz see the code..

this is decode...
/*  public static string Decode(string s1, string s2)
        {
            int j;            
            while (s1.IndexOf(s2)!=-1)
            {
                j = s1.IndexOf(s2);    
                
               s1 = s1.Remove(j, s2.Length);                                    
            }

            return s1;

        }*/



Problem is here...

public static string Encode(string s4, string s5)
        {
             
                Random rnd = new Random();

                int j = rnd.Next(20);
                
             for (int i = 0; i < s4.Length; i++)
                {
                    s4 = s4 + s5;//this will run infinite time bec i m increasing string length... 
                }
                
            
            return s4;
        }



any idea.... thanks

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: string manipulation

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: string manipulation

Posted 21 October 2008 - 08:33 AM

Try looping through the length of s5 since that's the string you appear to be decoding

for (int i = 0; i < s5.Length; i++)
{
       s4 += s5;//this will run infinite time bec i m increasing string length... 
}


Was This Post Helpful? 0
  • +
  • -

#3 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: string manipulation

Posted 21 October 2008 - 08:47 AM

View PostPsychoCoder, on 21 Oct, 2008 - 08:33 AM, said:

Try looping through the length of s5 since that's the string you appear to be decoding

for (int i = 0; i < s5.Length; i++)
{
       s4 += s5;//this will run infinite time bec i m increasing string length... 
}



Thanks for formating actually i m new to forums ...

anyway that will not solve the problem bec i have to add string s5 in s4 at random places... and seeing this new code u have written it will just add string s5 next to s4....same like s4+s5.... which is not my solution... thinking thinking thinking ............. lol
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: string manipulation

Posted 21 October 2008 - 11:00 PM

View Postafridi2129, on 21 Oct, 2008 - 08:10 AM, said:

hi.. there is 2 fucntion encoding and decoding of 2 strings... in nencoding i have to put one string in another at random places... and in decoding i have to get the original string..

decoding is done.. but have some problem in encoding.. plz see the code..

this is decode...
/*  public static string Decode(string s1, string s2)
        {
            int j;            
            while (s1.IndexOf(s2)!=-1)
            {
                j = s1.IndexOf(s2);    
                
               s1 = s1.Remove(j, s2.Length);                                    
            }

            return s1;

        }*/



Problem is here...

public static string Encode(string s4, string s5)
        {
             
                Random rnd = new Random();

                int j = rnd.Next(20);
                
             for (int i = 0; i < s4.Length; i++)
                {
                    s4 = s4 + s5;//this will run infinite time bec i m increasing string length... 
                }
                
            
            return s4;
        }



any idea.... thanks

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

public static string Encode(string s4, string s5)
{             
//   Random rnd = new Random();
//   int j = rnd.Next(20);

     int lengthofs4 = s4.Length;

     for (int i = 0; i < lengthofs4 ; i++)
     {
         s4 = s4 + s5; // resultant length dependent on original length
     }

     return s4;
}


Was This Post Helpful? 0
  • +
  • -

#5 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: string manipulation

Posted 22 October 2008 - 04:03 AM

View Postn8wxs, on 21 Oct, 2008 - 11:00 PM, said:

View Postafridi2129, on 21 Oct, 2008 - 08:10 AM, said:

hi.. there is 2 fucntion encoding and decoding of 2 strings... in nencoding i have to put one string in another at random places... and in decoding i have to get the original string..

decoding is done.. but have some problem in encoding.. plz see the code..

this is decode...
/*  public static string Decode(string s1, string s2)
        {
            int j;            
            while (s1.IndexOf(s2)!=-1)
            {
                j = s1.IndexOf(s2);    
                
               s1 = s1.Remove(j, s2.Length);                                    
            }

            return s1;

        }*/



Problem is here...

public static string Encode(string s4, string s5)
        {
             
                Random rnd = new Random();

                int j = rnd.Next(20);
                
             for (int i = 0; i < s4.Length; i++)
                {
                    s4 = s4 + s5;//this will run infinite time bec i m increasing string length... 
                }
                
            
            return s4;
        }



any idea.... thanks

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

public static string Encode(string s4, string s5)
{             
//   Random rnd = new Random();
//   int j = rnd.Next(20);

     int lengthofs4 = s4.Length;

     for (int i = 0; i < lengthofs4 ; i++)
     {
         s4 = s4 + s5; // resultant length dependent on original length
     }

     return s4;
}




sir i think this
/*
 int lengthofs4 = s4.Length;

	 for (int i = 0; i < lengthofs4; i++)
*/



and This
for(int i=0;i<s4.length;i++)  


have the same menaing ... then why should i use extra variable int lengthofs4........

anyway this is not the problem.. the thng is that we have to insert string s5 in string s4 at different location..[indexes]

the problem is where should i use that variable j... anyway thanks for replies.....

This post has been edited by afridi2129: 22 October 2008 - 04:05 AM

Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: string manipulation

Posted 22 October 2008 - 04:14 AM

I don't think they are same. As you said, length of s4 is changing inside loop after every iteration, so your condition of for loop also changes every iteration. but if you store initial length of s4 in some variable and compare it with your index value in for loop condition, then even if your length of s4 changes afterwards, your for loop will run only for some definite iterations. That is up to the initial length of your s4.

These two things will be same only if length of s4 is not changing inside for loop.
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: string manipulation

Posted 22 October 2008 - 05:09 PM

View Postafridi2129, on 22 Oct, 2008 - 04:03 AM, said:

sir i think this
int lengthofs4 = s4.Length;

for (int i = 0; i < lengthofs4; i++)


and This
for(int i=0;i<s4.length;i++)  


have the same menaing ... then why should i use extra variable int lengthofs4........
anyway this is not the problem.. the thng is that we have to insert string s5 in string s4 at different location..[indexes]
the problem is where should i use that variable j... anyway thanks for replies.....

No they are not the same. Strings in C# are immutable. When strings are concatenated s4 += s5
the s4 "label" refers to a new s4 object. The loop test will reference the *current* definition of s4,
not the original one.

I have no idea where the variable j should be used because I don't have mystic vision. What is
the problem you are trying to solve?
Was This Post Helpful? 0
  • +
  • -

#8 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: string manipulation

Posted 23 October 2008 - 05:37 AM

View Postn8wxs, on 22 Oct, 2008 - 05:09 PM, said:

View Postafridi2129, on 22 Oct, 2008 - 04:03 AM, said:

sir i think this
int lengthofs4 = s4.Length;

for (int i = 0; i < lengthofs4; i++)


and This
for(int i=0;i<s4.length;i++)  


have the same menaing ... then why should i use extra variable int lengthofs4........
anyway this is not the problem.. the thng is that we have to insert string s5 in string s4 at different location..[indexes]
the problem is where should i use that variable j... anyway thanks for replies.....

No they are not the same. Strings in C# are immutable. When strings are concatenated s4 += s5
the s4 "label" refers to a new s4 object. The loop test will reference the *current* definition of s4,
not the original one.

I have no idea where the variable j should be used because I don't have mystic vision. What is
the problem you are trying to solve?


Hi.. Sir actually the problem is that we have to encode a string in another string at different locations.(random locations).. like if a s1="NISAR"; and s2="*a*";
then i have to put s2 in s1 at different random locations.
and say it will became like this. N*a*i*a*SA*a*R....

now i have to decode it to its origional string which is NISAR... this is very funny thing that i have done decoding but cant do encoding.....in decoding i m using 2 other strings.. problem is with encoding...
Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: string manipulation

Posted 23 October 2008 - 06:33 PM

View Postafridi2129, on 23 Oct, 2008 - 05:37 AM, said:

Hi.. Sir actually the problem is that we have to encode a string in another string at different locations.(random locations).. like if a s1="NISAR"; and s2="*a*";
then i have to put s2 in s1 at different random locations.
and say it will became like this. N*a*i*a*SA*a*R....

now i have to decode it to its origional string which is NISAR... this is very funny thing that i have done decoding but cant do encoding.....in decoding i m using 2 other strings.. problem is with encoding...

The following is not what I would do. :)
private static string Encode(string s4, string s5)
{
    Random rnd = new Random();

    int lengthofs4 = s4.Length;

    for (int i = 0; i < lengthofs4; i++)
        s4 = s4.Insert(rnd.Next(0, lengthofs4), s5);

    return s4;
}


Was This Post Helpful? 0
  • +
  • -

#10 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: string manipulation

Posted 24 October 2008 - 07:44 AM

View Postn8wxs, on 23 Oct, 2008 - 06:33 PM, said:

View Postafridi2129, on 23 Oct, 2008 - 05:37 AM, said:

Hi.. Sir actually the problem is that we have to encode a string in another string at different locations.(random locations).. like if a s1="NISAR"; and s2="*a*";
then i have to put s2 in s1 at different random locations.
and say it will became like this. N*a*i*a*SA*a*R....

now i have to decode it to its origional string which is NISAR... this is very funny thing that i have done decoding but cant do encoding.....in decoding i m using 2 other strings.. problem is with encoding...

The following is not what I would do. :)
private static string Encode(string s4, string s5)
{
    Random rnd = new Random();

    int lengthofs4 = s4.Length;

    for (int i = 0; i < lengthofs4; i++)
        s4 = s4.Insert(rnd.Next(0, lengthofs4), s5);

    return s4;
}



Sir Thanx alot for your reply... but i have done somthng same last night... see next post..
Was This Post Helpful? 0
  • +
  • -

#11 afridi2129  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: string manipulation

Posted 24 October 2008 - 07:52 AM

Ok now closing this topic..Solution i have submitted today is.
using System;
using System.Collections.Generic;
using System.Text;

namespace Fuiems.Se5.OOSE.Nisar.Assignment1
{
	class Program
	{
		static void Main(string[] args)
		{
			
			string s4="AFRIDI2129"; 
			string s5 = "*a*";
		   
			string s3;
 
		   
			
			s3=Encode(s4,s5);  //calling encoder fucntion
			Console.Write(s3); 
			Console.WriteLine();

			s3 = Decode(s3, s5); 
		  //calling decoder function for encoded value s3

			Console.Write(s3);
			Console.WriteLine();
 
		}

		public static string Encode(string s4, string s5)
		{

				int y = 3; //a variable used for random 
 
				Random rnd = new Random();

			   
				 
				 for (int i = 0; i <s5.Length; i++)
					{
					  int j = rnd.Next(y,10); 
//(y,10) means that from 3 to 10  choose any random location
						 
   s4 = s4.Insert(j, s5); //j is the starting index ,s5 is  
						   //the string

						 
					}
			   
			   
			return s4;
		}

		



public static string Decode(string s1, string s2) 
		{
		  int j;			
		  while (s1.IndexOf(s2)!=-1)//will run till j=-1
		
		  for(int i=0;i<s2.Length;i++)
			{
			  j = s1.IndexOf(s2);   
  //getting the index of s2 starting place in s1
				
  s1 = s1.Remove(j, s2.Length);   
   //just calling a remove  method to  remove s2 from s1								
			}

			return s1;

		}
	}
}




It may be not the perfect one.. but my try.

This post has been edited by afridi2129: 24 October 2008 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1