extracting integer from the given string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Iniya Alan
    New Member
    • Nov 2019
    • 2

    extracting integer from the given string

    question:
    the program must accept a string s as intput.the program must print the sum of all two or three digit numbers present in the string s as output.
    input: i got up at 10:100am and went to college at 12222:50 10 pm
    output:
    10+100+50+10=17 0
    how can i extract the integers from the given string??
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    A string of input is provided by the user. To extract the numbers from the string we can scan every character from the string and compare it with the numeric values. If the character is a digit, we can keep copying it to a temporary character array. Stop this process once a non-digit character is found. By this, we would have the number in the temporary character array. After that conversion from string to int can be done using the library function int atoi(const char *str). This process can be repeated for each number present in the string. For the addition of the values, take a variable sum with the initial value of 0. And the expression sum=sum+tempora ry_var can be used to keep adding those values.

    Comment

    • cactusdata
      Recognized Expert New Member
      • Aug 2007
      • 223

      #3
      A simple loop will manage this:

      Code:
      string input = "I got up at 10:100am and went to college at 12222:50 10 pm";
      
      string digits = string.Empty;
      int total = 0;
      bool first = true;
      foreach (char character in (input + " "))
      {
          if (!Char.IsDigit(character))
          {		
              if (digits.Length > 0 && digits.Length <=3)
              {
                  if (!first)
                  {
                      Console.Write("+");
                  }
                  Console.Write(digits);
                  total += Convert.ToInt32(digits);
                  first = false;
              }
              digits=string.Empty;
          }
          else
          {
              digits += character;
          }
      }
      Console.WriteLine("=" + total.ToString());

      Comment

      • Iniya Alan
        New Member
        • Nov 2019
        • 2

        #4
        Thanks sir,its clear to me but I want some c code examples for comparision of numeric values and finding the digit

        Comment

        • dev7060
          Recognized Expert Contributor
          • Mar 2017
          • 656

          #5
          @cactusdata The question is asked in the C/C++ category, not in the C sharp one. It would have benefitted the user if you had posted the algorithm/approach instead. It doesn't even make more sense when the solution is to be implemented in C language, where there is no concept of classes and whole object-oriented programming stuff.

          @Iniya What have you done so far? Comparisons are simple using the if statements.
          Code:
          if(string_name[index]=='1'){
          ...
          }

          Comment

          • cactusdata
            Recognized Expert New Member
            • Aug 2007
            • 223

            #6
            Missed the C/C++, sorry, but my C# code should be easy to convert.

            Comment

            Working...