none
convert C to C# RRS feed

  • Question

  • I need one struct in c convert to c#. struct given below
    Please help me out on this.

    int main()

    {

         char a[101],s[101];

         int i,len,mid,next,top;

         gets(a);

         len=strlen(a);

         mid=len/2-1;

         top=0;

        for(i=0;i<=mid;i++)

               s[++top]=a[i];

        if(len%2==0)

             next=mid+1;

        else

              next=mid+2;

       for(i=next;i<=len-1;i++)

       {

                if(a[i]!=s[top])

                          break

                top--;

    }

    if(top==0)

    print("YES");

    else

    print("NO")'

     getchar();getchaer();

    return 0;

    }

    Wednesday, November 22, 2017 11:12 AM

Answers

  • Hi SouKyuSein,

    Could be like below You have to change it as per your requirement:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TTEH
    {
        class Program
        {
            static void Main(string[] args)
            {          
    
                string[] a = new string[101];
                string[] s = new string[101];
                int i, len, mid, next, top;
    
                Console.WriteLine("Enter value of a: ");
    
                for (i = 0; i < a.Length; i++)
                {
                    a[i] = Console.ReadLine();
                }
    
                len = a.Length;
                mid = len / 2 - 1;
                top = 0;
    
                for (i = 0; i <= mid; i++)
                {
                    s[++top] = a[i];
                }
    
                if (len % 2 == 0)
                {
                    next = mid + 1;
                }
                else
                {
                    next = mid + 2;
                }
    
                for (i = next; i <= len - 1; i++)
                {
                    if (a[i] != s[top])
                        break;
                    top--;
                }   
              
                if(top==0)
                {
                    Console.WriteLine("YES");
                }
                else
                {
                    Console.WriteLine("NO");
                }
            }        
        }
    }
    


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by SouKyuSein Wednesday, November 22, 2017 6:39 PM
    Wednesday, November 22, 2017 1:04 PM
    Moderator

All replies

  • sorry i am a begineer to learn C and C#
    Wednesday, November 22, 2017 11:12 AM
  • Hi SouKyuSein,

    Could be like below You have to change it as per your requirement:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TTEH
    {
        class Program
        {
            static void Main(string[] args)
            {          
    
                string[] a = new string[101];
                string[] s = new string[101];
                int i, len, mid, next, top;
    
                Console.WriteLine("Enter value of a: ");
    
                for (i = 0; i < a.Length; i++)
                {
                    a[i] = Console.ReadLine();
                }
    
                len = a.Length;
                mid = len / 2 - 1;
                top = 0;
    
                for (i = 0; i <= mid; i++)
                {
                    s[++top] = a[i];
                }
    
                if (len % 2 == 0)
                {
                    next = mid + 1;
                }
                else
                {
                    next = mid + 2;
                }
    
                for (i = next; i <= len - 1; i++)
                {
                    if (a[i] != s[top])
                        break;
                    top--;
                }   
              
                if(top==0)
                {
                    Console.WriteLine("YES");
                }
                else
                {
                    Console.WriteLine("NO");
                }
            }        
        }
    }
    


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by SouKyuSein Wednesday, November 22, 2017 6:39 PM
    Wednesday, November 22, 2017 1:04 PM
    Moderator
  • thank you

    can you tell me why you didn't use "gets(a)" and "char a[101] ,char b[101]" in C#?


    Wednesday, November 22, 2017 1:39 PM
  • Hi SouKyuSein,

    In C# user input is taken by using Console.ReadLine()
    You can also use "char" in C# it depends user preference. 
    Char declaration in C# is little different. Please refer to following to learn about "char" in C#

    Char Type

    Char Array


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, November 24, 2017 9:40 AM
    Moderator