heeeeeeeeeeelppppppppppp - I'm new and stuck

Hello there people. I am new to C# programming and i am struggling to create a console app that would total the ineger values of each chacrachter in a string no including the spaces. The first string being a fixed string and the second string a max lenght of 80 characters, to give to seperate totals of each charachter in the string. uff so confused really wanted to do c# but i broke me leg an fell behind with me work close to dropping it :/ any help would be a great help thnx...

Comments

  • : Hello there people. I am new to C# programming and i am struggling
    : to create a console app that would total the ineger values of each
    : chacrachter in a string no including the spaces. The first string
    : being a fixed string and the second string a max lenght of 80
    : characters, to give to seperate totals of each charachter in the
    : string. uff so confused really wanted to do c# but i broke me leg an
    : fell behind with me work close to dropping it :/ any help would be a
    : great help thnx...
    :

    Not sure what you're looking to do here, but you can use
    System.Convert.ToInt32(...), string.substring & string.Length to convert bits of strings to numbers, and you can use System.Text.Encoding.Default.GetBytes(...) and System.Text.Encoding.Default.GetString(...) to convert between strings and arrays of bytes. That should be enough to get it done.


  • [hr]
    Sorry to hear about your leg....get well soon!

    [color=Purple]I too, am very new to programming in C# and I think it's great that you also want to begin programming in C Sharp. As a newbie, there were some suggestions given to me from some of the pro
  • First off thanks for the info itechxxiv, it made alot of sense to me if anything else did not :). Ok I'll attempt to put my previous question in a better format.

    [color=Green]// Create a Fixed String, which i think is done as so..[/color]
    [color=Blue]const string [/color]scentenceOne = "the quick red fox jumped over the moon"

    [color=Green]//total the integer values of each charachter in the above string[/color]

    [color=Green]//Display total of scentenceOne in its numerical format[/color]
    [color=Blue]Console[/color].WriteLine("?") // cant figure out the above so this is confusing.

    next....
    [color=Green]//input another string with 80 charachters max lentgh[/color]
    scentence2 = strscentence2.ToString(Console.ReadLine())//so confused here

    //out put the result of the second scentence
    [color=Blue]Console[/color].WriteLine("?") //again no idea what to put here

    [color=Blue]Console[/color].ReadLine() //Wait for user input

    The problems i face are listed below
    [color=Red]1.I don't know how to convert a string to its numerical value.
    2.I can't figure out how to get the program to add each charachter in the scentence togther to make a numerical total.
    3.I am not sure how to obtain a string from a users input with a max lentgh of 80 chars.[/color]
    all i know is something simple like using && to add 'A' and 'B' togther hehe... hope this helps people understand it better if not please do nag at me, as every nag is a learning stage :)
  • : First off thanks for the info itechxxiv, it made alot of sense to me
    : if anything else did not :). Ok I'll attempt to put my previous
    : question in a better format.
    :
    : [color=Green]// Create a Fixed String, which i think is done as
    : so..[/color]
    : [color=Blue]const string [/color]scentenceOne = "the quick red fox
    : jumped over the moon"
    :
    : [color=Green]//total the integer values of each charachter in the
    : above string[/color]
    :
    : [color=Green]//Display total of scentenceOne in its numerical
    : format[/color]
    : [color=Blue]Console[/color].WriteLine("?") // cant figure out the
    : above so this is confusing.
    :
    : next....
    : [color=Green]//input another string with 80 charachters max
    : lentgh[/color]
    : scentence2 = strscentence2.ToString(Console.ReadLine())//so confused
    : here
    :
    : //out put the result of the second scentence
    : [color=Blue]Console[/color].WriteLine("?") //again no idea what to
    : put here
    :
    : [color=Blue]Console[/color].ReadLine() //Wait for user input
    :
    : The problems i face are listed below
    : [color=Red]1.I don't know how to convert a string to its numerical
    : value.
    : 2.I can't figure out how to get the program to add each charachter
    : in the scentence togther to make a numerical total.
    : 3.I am not sure how to obtain a string from a users input with a max
    : lentgh of 80 chars.[/color]
    : all i know is something simple like using && to add 'A' and 'B'
    : togther hehe... hope this helps people understand it better if not
    : please do nag at me, as every nag is a learning stage :)
    :

    So are you trying to count how many letter "a"'s you have, and how many letter "b"s and so on, or are you to convert each letter to its ASCII value and add up the whole sentence, giving you just one number?

    If it's the first problem, a simple way to do it is to have an array of 26 counters set up, one for each letter, and you need to loop over each character in the sentence, and add one to the correct counter depending on the letter.

    If you can work out how to do that, a better way to do it would be to have a variable number of totals (as you might not have one of each type of letter, or you might have more than 26 types of letters) and each total would keep track itself of what kind of letter or character it applied to.

    If it's converting the whole string to its corresponding ASCII numbers and adding them up, then you can convert it to a byte array using System.Text.Encoding.Default.GetBytes and then loop over this aray and add up to get your total.

    It looks like you're trying to do the same computation on both the fixed string and the variable length one. If so you should put your computations in a subroutine so you only have to write that part once:

    Declare your fixed string
    DoSumsAndPrintOutput(fixedString)
    Read in variable string
    DoSumsAndPrintOutput(variableString)

    My tea breaks over, good luck :)
  • : First off thanks for the info itechxxiv, it made alot of sense to me
    : if anything else did not :). Ok I'll attempt to put my previous
    : question in a better format.
    :
    : [color=Green]// Create a Fixed String, which i think is done as
    : so..[/color]
    : [color=Blue]const string [/color]scentenceOne = "the quick red fox
    : jumped over the moon"
    :
    : [color=Green]//total the integer values of each charachter in the
    : above string[/color]
    :
    : [color=Green]//Display total of scentenceOne in its numerical
    : format[/color]
    : [color=Blue]Console[/color].WriteLine("?") // cant figure out the
    : above so this is confusing.
    :
    : next....
    : [color=Green]//input another string with 80 charachters max
    : lentgh[/color]
    : scentence2 = strscentence2.ToString(Console.ReadLine())//so confused
    : here
    :
    : //out put the result of the second scentence
    : [color=Blue]Console[/color].WriteLine("?") //again no idea what to
    : put here
    :
    : [color=Blue]Console[/color].ReadLine() //Wait for user input
    :
    : The problems i face are listed below
    : [color=Red]1.I don't know how to convert a string to its numerical
    : value.
    : 2.I can't figure out how to get the program to add each charachter
    : in the scentence togther to make a numerical total.
    : 3.I am not sure how to obtain a string from a users input with a max
    : lentgh of 80 chars.[/color]
    : all i know is something simple like using && to add 'A' and 'B'
    : togther hehe... hope this helps people understand it better if not
    : please do nag at me, as every nag is a learning stage :)
    :
    [hr]

    Hi again, it
  • great feedback guys. This has given me a whole new leaf about how it could possibly work, i'll keep cracking at it and see if i can figure it out to.
  • oh and semley's question im tryin to do this, "convert each letter to its ASCII value and add up the whole sentence, giving just one number"

  • so far i have come up with this but this code only displays the ascii values to the screen, not including the value of the spaces.

    //Declare a string
    string s = "hallo world";

    //Get ASCII Values
    foreach (char c in s)
    {
    //Test For Spaces
    if (c == 32)
    Console.WriteLine("");
    else
    Console.WriteLine(System.Convert.ToInt32(c));
    }
    Console.ReadLine();

    now what i would like to do is sumhow add these values togther but have no idea in what kind of order the code should be. in general i do not want to display the ascii values to the screen i just want to add them togther to produce one total consisting of all the asci values added up. hope sum1 can help figure this out its driving me nuts
  • : so far i have come up with this but this code only displays the
    : ascii values to the screen, not including the value of the spaces.
    :
    : //Declare a string
    : string s = "hallo world";
    :
    : //Get ASCII Values
    : foreach (char c in s)
    : {
    : //Test For Spaces
    : if (c == 32)
    : Console.WriteLine("");
    : else
    : Console.WriteLine(System.Convert.ToInt32(c));
    : }
    : Console.ReadLine();
    :
    : now what i would like to do is sumhow add these values togther but
    : have no idea in what kind of order the code should be. in general i
    : do not want to display the ascii values to the screen i just want to
    : add them togther to produce one total consisting of all the asci
    : values added up. hope sum1 can help figure this out its driving me
    : nuts
    :
    This is the general code of adding numbers together:
    [code]
    sum = 0; // initialize the sum
    for each number {
    sum = sum + number; // add the number to the sum
    }
    [/code]
    This is exactly the same as you would do by hand.
    If you already have a a loop, it is possible to combine those into one loop.
  • I've cracked it so i thought i would put my completed code here.
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace charcTotals
    {
    class Program
    {
    static void Main(string[] args)
    {

    //Declare a fixed string, and a empty string
    int total = 0, total2 = 0, count = 0;
    string strOne = "the quick red fox jumped over the lazy brown dog", strTwo;


    //Convert ASCII Values of String strOne
    foreach (char c in strOne)
    {
    total += (Convert.ToInt32(c));
    //Test for spaces and deduct From Total
    if (c == 32)

    total -= 32;
    }
    //Output the Total to the user
    Console.WriteLine("The total of the ASCII Values in the string:
    '{0}'

    Total To: {1} 'Not including spaces'
    ", strOne, total);

    //Get empty string strTwo from the user, and check lenght does not exeed 80 chars
    do
    {
    count = 0;
    Console.WriteLine("Please Enter a String including alphanumeric and numeric charachters:");
    strTwo = Console.ReadLine();
    foreach (char D in strTwo)
    count++;

    } while (count <= 0 || count > 80);

    //Convert S2 to ASCII Values
    foreach (char c in strTwo)
    {
    total2 += (Convert.ToInt32(c));
    //Test for spaces and remove from total
    if (c == 32)

    total2 -= 32;
    }

    Console.WriteLine("
    The total of the ASCII Values in the string:
    '{0}'

    Total To: {1} 'Not including spaces'
    ", strTwo, total2);

    Console.WriteLine("Press ENTER to continue");
    Console.ReadLine();
    }
    }
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories