473,379 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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=170
how can i extract the integers from the given string??
Nov 29 '19 #1
5 4411
dev7060
636 Expert 512MB
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+temporary_var can be used to keep adding those values.
Dec 1 '19 #2
cactusdata
214 Expert 128KB
A simple loop will manage this:

Expand|Select|Wrap|Line Numbers
  1. string input = "I got up at 10:100am and went to college at 12222:50 10 pm";
  2.  
  3. string digits = string.Empty;
  4. int total = 0;
  5. bool first = true;
  6. foreach (char character in (input + " "))
  7. {
  8.     if (!Char.IsDigit(character))
  9.     {        
  10.         if (digits.Length > 0 && digits.Length <=3)
  11.         {
  12.             if (!first)
  13.             {
  14.                 Console.Write("+");
  15.             }
  16.             Console.Write(digits);
  17.             total += Convert.ToInt32(digits);
  18.             first = false;
  19.         }
  20.         digits=string.Empty;
  21.     }
  22.     else
  23.     {
  24.         digits += character;
  25.     }
  26. }
  27. Console.WriteLine("=" + total.ToString());
Dec 2 '19 #3
Thanks sir,its clear to me but I want some c code examples for comparision of numeric values and finding the digit
Dec 2 '19 #4
dev7060
636 Expert 512MB
@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.
Expand|Select|Wrap|Line Numbers
  1. if(string_name[index]=='1'){
  2. ...
  3. }
  4.  
Dec 2 '19 #5
cactusdata
214 Expert 128KB
Missed the C/C++, sorry, but my C# code should be easy to convert.
Dec 2 '19 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: sam | last post by:
Hi, Does STL has Integer class? I want to convert an integer to string. Of course I can use sprintfs C API, but I m wondering whether STL has Integer class to be utilsed. Thanks Sam.
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
1
Jugmike
by: Jugmike | last post by:
Hi I want to have a format while conversion from integer to string i want that string has 5 digits and if integer has 3 digits only then it is represented like 00122. There is programming which i...
2
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
1
by: redgrL86 | last post by:
Hi, I am trying to figure out how to see if any of a given set of childnodes equals a given string. For example, in the XML and XSL code below, I want the text in the "xsl:when" statement to output...
3
by: Krupa Kiran | last post by:
Hi i have function in script. I did a small calculation in that function when i try to pass a value to in Field thet only passes integer value. But i need to pass both String and the integer value....
4
by: mthread | last post by:
Hi, I would like to know the method to convert integer to string. Thanx in advance.
8
by: rajeevs | last post by:
Hi Is there a way we can generate random alphabet combination from a given string? I am looking for a 3 letter combination from a given string by using vba. Before or after generating it should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.