473,399 Members | 3,302 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,399 software developers and data experts.

how do I convert a string to char array to find a substring within a string?

2
I am trying to find a string within a substring using strstr. This only works with char arrays. I have an array of objects of which I am searching a particular property which is a string.
Expand|Select|Wrap|Line Numbers
  1. char *strPtr;
  2.  
  3.     int index = 0;
  4.     bool located = false;
  5.  
  6.     do
  7.     {
  8.         for(index = 0; index <NUM_BOOKS; index ++)
  9.         {
  10.             strPtr = strstr(bookList[index].getTitle(), book);//str1, str2);
  11.             if(strPtr == NULL)
  12.             {
  13.                 cout << "No matching titles were found.\n";
  14.             }
  15.             else
  16.             {
  17.                 located = true;
  18.                 found.push_back(index);
  19.             }
  20.         }
  21.  
  22.     }while(index < NUM_BOOKS && !located);
  23.  
I am using C++
Mar 9 '10 #1
4 3743
whodgson
542 512MB
If you declare string s you can then use getline(cin,s) to load a string to s from the keyboard.
You can print the string with cout<<s;
string s1;//declare s1.
You can get a substring from s with for example s1=s.substr(5,14) s1 gets the 6th element in the array s[] plus the next 13 characters including spaces.
cout<<s1;//prints s1 which is 14 characters long starting with the 6th element in array s.
Mar 10 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
This is C++ and not C so don't use strstr.

What you want is the find_first_of algorithm. You pass in your collection of objects plus a function pointer to a function that returns a bool if your search criteria are met or not.
Mar 10 '10 #3
red222
2
@weaknessforcats
Do you have any examples you could show me? I'm not exactly sure what you are saying.
Mar 15 '10 #4

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

Similar topics

6
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
5
by: Alan Silver | last post by:
Hello, <dumb question> I am trying to do something similar to the following... string str = "Hello"; char c = (char)str.Substring(2,1); but the compiler complains that it cannot convert...
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.