You are given a text file which contains any type of characters. You have to find the sum of integer values.
Input : text.txt Let contents of test.txt be : :-,,$%^5313&^*1)(*( 464sz29>>///11!!! (*HB%$#)(*0900 Output : 6727 Input : text1.txt Let contents of test.txt be : 234***3r3r() ()(0)34 Output : 274
1. To open the file in only read mode we can use ifstream library
2. Iterate through all rows of the file and find all integers
3. If integer found then store it in temp variable because there is a chance that next character can be an integer
4. Add temp value in final result if any character found except integers and set temp = 0 again
5. If last element is an integer then we have to add temp after loop if it isn’t then the value of temp will already be zero. So sum will not affected.
6727 (for Input 1)
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print "Hello World" in C/C++ without using any header file
- C program to copy contents of one file to another file
- Four File Handling Hacks which every C/C++ Programmer should know
- How to sum two integers without using arithmetic operators in C/C++?
- C Program to find size of a File
- iscntrl() in C++ and its application to find control characters
- Find elements of an Array which are Odd and Even using STL in C++
- Find elements of an array which are divisible by N using STL in C++
- Extract all integers from string in C++
- Implementing ternary operator without any conditional statement
- Is there any need of "long" data type in C and C++?
- Programming puzzle (Assign value without any control statement)
- Any datatype in C++ boost library
- bitset any() in C++ STL
- Is there any equivalent to typedef of C/C++ in Java ?
- std::any Class in C++
- fopen() for an existing file in write mode
- Implement your own tail (Read last n lines of a huge file)
- lseek() in C/C++ to read the alternate nth byte and write it in another file
- Write a C program that displays contents of a given file like 'more' utility in Linux