Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Few problems ... Algorithm and Data Structure

  1. #1
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Few problems ... Algorithm and Data Structure

    Hello Mate .. First of all thanks to all helpers here .. This community seems to be a place where i can find my answer ..

    I m a beginner and learned basic of c# ... currently doing Algorithm and Data Structures ... I m chapter there is are few problems which sucks my mind and i cant understand what to do ...

    In first problem we have to accept two strings and check whether the second strings exists within the first string .. Like .. if 1st string is "concatenation" and the second string is "cat" .. the program should display - Substring found at position 4 ... and if 2nd string is "tent" the it should show .. -- Substring not found ...

    I also have to write an algorithm, will you guys please help me ...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest
    Can you post your code please? It is a fairly simple thing to do:

    Code:
     string myString = "concatenation"; 
      string searchString = "cat"; 
      int found = myString.IndexOf(SearchString); 
    MessageBox.Show("Found at : " + found );

  4. #3
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    Thanks a lot mate .. actually i ain't made the code because i cant understand how to declare variable for that..thanks again mate ..

  5. #4
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    I just now modified your code a bit and prepared a c# program..

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Enter your string");
    string myString = Console.ReadLine();
    Console.WriteLine("Enter your search string");
    string searchString = Console.ReadLine();
    int found = myString.IndexOf(searchString);
    while (true)
    {
    Console.WriteLine("Substring Found at : " + (found + 1));
    break;
    }
    {
    Console.WriteLine("Substring Not Found");
    }
    Console.ReadLine();
    }
    }
    }
    Now .. when i run this program it works absolutely fine .. just a problem ..

    when substring not found it shows ..

    Substring found at : 0
    Substring not found
    I only need to display ... substring not found .. not the first line .. please check ..
    Attached Files Attached Files
    Last edited by nt_virus; 02-10-2008 at 09:21 PM.

  6. #5
    jireh is offline Newbie
    Join Date
    Jan 2008
    Posts
    18
    Rep Power
    15
    Code:
    if (searchString<>string.Empty)
      {
           Console.WriteLine("Substring Found at : " + (found + 1));
      }
    else
     { 
           Console.WriteLine("Substring Not Found");
     }
    how baout that, huh?

    enjoy coding...

    regards,

    jireh

  7. #6
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    @ jireh, Thanks for the help mate ... First i think ... this sign .. <> means not equals to .. isn't ? But showing error when i use that sign ... or might be i m missing something ... this sign .. != works fine for not equals to.. anyway ..

    From your code... if substring is not found its showing ...

    Substring Found at : 0

    .....

    anyway after few thinking and experiment .. i m able to make this program to work successfully ...

    The code for perfect program is ...

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication2
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Enter Main String");
    string myString = Console.ReadLine();
    Console.WriteLine("Enter SubString");
    string searchString = Console.ReadLine();
    int found = myString.IndexOf(searchString);
    if (found < 1)
    {
    Console.WriteLine("Substring Not Found");
    }
    else
    {
    Console.WriteLine("Substring Found at position : " + (found + 1));
    }



    Console.ReadLine();

    }
    }
    }
    check below attachment for exe...
    Attached Files Attached Files

  8. #7
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    ... in the above code .. still there is some problem.... if we look for ca in cat ... the substring found at index 0 .. so if loop treating as True and printing substring not found ... please anyone suggest me what to do now ..

  9. #8
    Jordan Guest
    Try setting found to less than 0:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter Main String");
                string myString = Console.ReadLine();
                Console.WriteLine("Enter SubString");
                string searchString = Console.ReadLine();
                int found = -1;
                found = myString.IndexOf(searchString);
                if (found < 0)
                {
                    Console.WriteLine("Substring Not Found");
                }
                else
                {
                    Console.WriteLine("Substring Found at position : " + (found + 1));
                }
    
                
    
                Console.ReadLine();
    
            }
        }
    }
    Last edited by Jordan; 02-13-2008 at 05:13 AM.

  10. #9
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    Sorry, i don't have any more words to say except this --- YOU ARE GENIUS !!!

  11. #10
    nt_virus is offline Newbie
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0
    Please remove case-sensitiveness .. i don't know how to do ...

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Data tree structure
    By theeno in forum Python
    Replies: 0
    Last Post: 01-14-2011, 12:22 PM
  2. tutorials for data structure using c
    By shiyamhoda in forum C and C++
    Replies: 2
    Last Post: 10-13-2010, 07:29 PM
  3. Help with math problems on the structure
    By DimaStudMaestro1 in forum C and C++
    Replies: 6
    Last Post: 05-13-2010, 12:25 PM
  4. data structure program in C++
    By Scobcode in forum Managed C++
    Replies: 15
    Last Post: 12-13-2008, 11:48 AM
  5. Text justification algorithm/structure help
    By geon in forum General Programming
    Replies: 5
    Last Post: 10-08-2008, 09:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts