473,408 Members | 1,771 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,408 software developers and data experts.

Checking for different strings

6
Hi all,
I am new to C++ and would like to get some help here.
I am trying to use C++ to read text files. From there, I wish to capture strings that coincide with certain declared strings.
For eg. Using getline(), I have a whole line of string from the text file. From there, I want to check if in that line, there exist "Gross Margin" in it. If it exist, I will want to capture the whole string and output it to another separate text file. Moreover, I need to check for the different type of case for Gross Margin. "Gross margin", and "gross margin" has to be checked together to get the result.

Hope I can get some help soon. Many thanks in advance.
Jan 10 '07 #1
3 1461
horace1
1,510 Expert 1GB
Hi all,
I am new to C++ and would like to get some help here.
I am trying to use C++ to read text files. From there, I wish to capture strings that coincide with certain declared strings.
For eg. Using getline(), I have a whole line of string from the text file. From there, I want to check if in that line, there exist "Gross Margin" in it. If it exist, I will want to capture the whole string and output it to another separate text file. Moreover, I need to check for the different type of case for Gross Margin. "Gross margin", and "gross margin" has to be checked together to get the result.

Hope I can get some help soon. Many thanks in advance.
have a look at the string tutorial
http://www.cprogramming.com/tutorial/string.html

and the reference
http://www.cppreference.com/cppstring/index.html
Jan 10 '07 #2
Ganon11
3,652 Expert 2GB
You can use the string function .find() as follows:

Once you have the input in a string variable (message, for example), if you know the string you want to search for, use

Expand|Select|Wrap|Line Numbers
  1. message.find(YourString); // in this case, "Gross Margin"
The function will return the integer representing the position of YourString in message. For instance, if message was "Yearly Gross Margin Is Blah", then message.find("Gross Margin") would return 7 because "Gross margin" starts at position 7 of message.

If YourString is not present in message, the .find() function returns an integer constant string::npos. So, if I were to search the message above for "Money", the .find() function would return string::npos.

It follows that, if YourString is present in message, than .find() does NOT return string::npos. You can check if message contains the given string by checking if its .find() value is NOT string::npos.

Note: You may have to #include <string> to get string::npos.
Jan 10 '07 #3
facade
6
Hi all,
Thanks for all the help. Will go try them out.

Regards
Jan 11 '07 #4

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

Similar topics

3
by: Steven Scaife | last post by:
Below is my ASP page, I have changed the update to read DRIAL which doesn't exist, shouldnt this throw an error, or if the connection cannot be made shouldnt it throw an error as well thanks in...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
4
by: Edd | last post by:
Hello, I have an array of strings containing filenames. I must open each in turn and parse the data within. However, if a filename appears multiple times in the list it must still only be read...
8
by: Brendan | last post by:
There must be an easy way to do this: For classes that contain very simple data tables, I like to do something like this: class Things(Object): def __init__(self, x, y, z): #assert that x,...
10
by: David | last post by:
I am trying to get this straight in my head so I can implement. I wrote a small utility some time ago in c# that utilized a handful of threads. That scenario was one main thread manually...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
9
by: John Zenger | last post by:
To my horror, someone pointed out to me yesterday that a web app I wrote has been prominently displaying a misspelled word. The word was buried in my code. Is there a utility out there that...
7
by: nospam | last post by:
Hello I need to go through each line of a CSV file, and extract some fields using a regex. Then, I need to check each retrieved field, and if it looks like "", turn this into NULL so that it's...
12
by: Joe Strout | last post by:
On Nov 12, 2008, at 10:45 AM, Tim Rowe wrote: Because if I write a method with the intention of treating the arguments like strings in various ways (slicing, combining with other strings,...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.