-1

There's a program I wrote in C, it works perfectly. When I tried to translate it into the language C# it was not compiling. The problem was that C# doesn't know the meaning of scanf ("%19s" , string1); like in C.
I changed scanf to:

string read;
do
{
     read = Console.ReadLine();
}
while (read.Length <= 19); 

it couldn't work properly. then i tried: to change it into:

string string1Input = Console.ReadLine();

It was working better but if statements were not checked because I was getting all "sum"-s equaled to 0.

This is the program written translated into C#:

public static class GlobalMembersAnbanisRicxvitiMnishvneloba
{
    static int Main()
    {
        string string1 = new string(new char[20]);   
        sbyte a = (sbyte)'a';
        sbyte b = (sbyte)'b';
        sbyte g = (sbyte)'g';
        sbyte X = (sbyte)'X';
        sbyte i = (sbyte)'i';
        sbyte H = (sbyte)'H';
        sbyte V = (sbyte)'V';
        etc..

        int rigi;
        int sum = 0;
        int sum2 = 0;
        int sum3 = 0;

        Console.Write(" my word is:\n");

        string string1Input = Console.ReadLine();

        for (rigi = 0; string1[rigi] != '\0'; rigi++)
        {
            if (string1[rigi] == a)
            {
                sum3 = sum3 + 1;
                sum2 = sum2 + 1;
                sum = sum + 1;
            }
            else
            if (string1[rigi] == b)
            {
                sum3 = sum3 + 3;
                sum2 = sum2 + 2;
                sum = sum + 2;
            }


               etc...
            } /* end for*/
    if (string1[rigi-1]==i) 
{
 sum=sum-10; sum2=sum2-10; sum3=sum3-55;
}
            Console.Write("sum is:");
            Console.Write("{0:D}\n", sum);
            Console.Write("sum2 is:");
            Console.Write("{0:D}\n", sum2);
            Console.Write("sum");
            Console.Write("{0:D}\n", sum3);
            return 0;
        }
    }

The idea of the prog is:
Program gives a numerical meaning to each of the letter of a word we type and adds this numbers to each other.

Question II: how to make

 if (string1[rigi-1]==i) 
    {
     sum=sum-10; sum2=sum2-10; sum3=sum3-55;
    } 

work?

Gio
  • 81
  • 1
  • 2
  • 9
  • 1
    This is why you should have done this by hand.... – Arran Apr 24 '13 at 10:03
  • 3
    Your code is based on a lot of assumptions that are true for C but not for C#. For example, strings in C# are not terminated by a `'\0'` character. Translating code requires in-depth knowledge of both languages; try to implement your program from scratch in C# if you're not deeply familiar with C# yet. – dtb Apr 24 '13 at 10:04
  • Why are there so many `a,b,g,H` etc. variables? `if (oneCharacter == 'a')` works just as well. – Joker_vD Apr 24 '13 at 10:57
  • this are letters of old Georgian alphabet and i wrote if statements 38 times – Gio Apr 24 '13 at 10:59

1 Answers1

0

There is no end caracter string in C#, like in C. In fact, string in C, and string in C# are two totally different things, and are not used the same way.

So for loop over a string, the good way is to use a foreach loop.
Change

for (rigi = 0; string1[rigi] != '\0'; rigi++)

By

foreach(var oneCaracter in string1Input)

Then string1[rigi] must become oneCaracter And sbyte must become char

Sample correction:

char a = 'a';
int sum = 0;
int sum2 = 0;
int sum3 = 0;

string string1Input = Console.ReadLine();

foreach(char oneCaracter in string1Input)
{
    if (oneCaracter == a)
    {
        sum3 = sum3 + 1;
        sum2 = sum2 + 1;
        sum = sum + 1;
    }
}

if (string1Input[string1Input.Length - 1] == i) 
{
    sum = sum - 10; sum2 = sum2-10; sum3 = sum3-55;
}
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
  • don't i have to initialize that `c`? or what if I have initialized letter c as `char c='c';` – Gio Apr 24 '13 at 10:15
  • @Gio : You are right, there will be two `c` variables declared. Give another name then. See my edited answer. – Cyril Gandon Apr 24 '13 at 10:18
  • thank u all it works. i used new value "M" i had not initialized it. now it works perfectly – Gio Apr 24 '13 at 10:25
  • this are changes i did. but how to represent after end of for `if (string1[rigi-1]==i) { sum=sum-10; sum2=sum2-10; sum3=sum3-55; }` /* if the last character is "i" then do this operation sum=sum-10; sum2=sum2-10; sum3=sum3-55;*/ – Gio Apr 24 '13 at 10:43
  • @Gio : not hard, look at the edited answer : `if (string1Input[string1Input.Length - 1] == i)` – Cyril Gandon Apr 24 '13 at 11:58