Need some help with char and string

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1005 Views - Last Post: 09 June 2009 - 02:10 PM Rate Topic: -----

#1 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 379
  • Joined: 27-December 08

Need some help with char and string

Post icon  Posted 08 June 2009 - 10:14 AM

Hi

i was having some problem with char and string
In a string we can search a word using find like this
#include<iostream>
#include<string>
int main()
{
  string me="I am best";
  int offset ;
  offset=me.find("best");
  std::cout<<offset;
  return 0 ;             //edited
}





So my Problem is how do I find a word in char array

Thanks for any help you provide

This post has been edited by prabh: 08 June 2009 - 10:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with char and string

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:24 AM

usually you wouldn't do that. it's not very efficient. hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 379
  • Joined: 27-December 08

Re: Need some help with char and string

Posted 08 June 2009 - 10:28 AM

View Postcomputerfox, on 8 Jun, 2009 - 09:24 AM, said:

usually you wouldn't do that. it's not very efficient. hope that helps :)

what do you mean
Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:33 AM

you're looking to find a word in an array of characters. normally you wouldn't do that because it's not efficient because why not just use a string then use a loop while checking each character. if this is an assignment from your teacher and he/she wants it with characters, then you would still do it with a loop going through the array. or you can cast the array into a string, but the point is you would use a loop just as if it were a normal string.
Was This Post Helpful? 0
  • +
  • -

#5 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:38 AM

Take a look here maybe.

http://www.cplusplus...cstring/strstr/

However, C strings are much more difficult to work with and manipulate than C++ stings.

This post has been edited by apw5020: 08 June 2009 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 379
  • Joined: 27-December 08

Re: Need some help with char and string

Posted 08 June 2009 - 10:39 AM

View Postcomputerfox, on 8 Jun, 2009 - 09:33 AM, said:

you're looking to find a word in an array of characters. normally you wouldn't do that because it's not efficient because why not just use a string then use a loop while checking each character. if this is an assignment from your teacher and he/she wants it with characters, then you would still do it with a loop going through the array. or you can cast the array into a string, but the point is you would use a loop just as if it were a normal string.



i got that what you want to say

now i know
what should i do

just tell me

that I'll loop through char array and than
suppose i want to convert one char array to string
i will do
 

string me;
char[20]="i am best";
for (int i=0 ;i<=20;i++)
{
char[i]+=string;
}



will this work
Was This Post Helpful? 0
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:42 AM

don't you think it should be something more like
string[i]=char[i]
?

what you think?
Was This Post Helpful? 0
  • +
  • -

#8 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:43 AM

How would that work?

This post has been edited by apw5020: 08 June 2009 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#9 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:47 AM

you guys don't read my posts do you? you can't do that because you need the final word equal the array. so it would be something like

finalWord[i]=finalWord+word[i];


This post has been edited by computerfox: 08 June 2009 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#10 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 379
  • Joined: 27-December 08

Re: Need some help with char and string

Posted 08 June 2009 - 10:50 AM

View Postcomputerfox, on 8 Jun, 2009 - 09:42 AM, said:

don't you think it should be something more like
string[i]=char[i]
?

what you think?

i don't want that string array
Was This Post Helpful? 0
  • +
  • -

#11 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:52 AM

*facepalm* it's not an array. it's just the position of the character in the string :wink:
Was This Post Helpful? 0
  • +
  • -

#12 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Need some help with char and string

Posted 08 June 2009 - 10:56 AM

To convert from a char array to a C++ string, do this:

char me[100] = "Hi how are you";
string converted(me);
cout << converted;



Or, you could look up the C string member functions (i.e. my first post).

This post has been edited by apw5020: 08 June 2009 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#13 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Need some help with char and string

Posted 08 June 2009 - 11:04 AM

or you can cast it. this is one of those things that there are several ways to accomplish what you want. it's up to you which on you want to do, but remember there are more sufficient ways to do it in that method. you can cast, loop, make a string and make the string equal to the array (which is casting really, but you're adding an unnecessary variable), or any other way like that.

@apw why do you keep bringing that up? i believe this is C++ why would you use an older method? i know C++ is built off of C, but why would you use an older method when there a modern one?

This post has been edited by computerfox: 08 June 2009 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Need some help with char and string

Posted 08 June 2009 - 11:42 AM

Good lord this thread is way off base :) Could the OP please restate your request? It read to me like you wanted to find a substring within a char array. If that is the case, use the strstr function as suggested above. If you want to assign a char array to a string object, you can simply assign it (the cast is implicit)
std::string str;
char array[] = "Hello World";

str = array;


If you want something else, please elaborate.
Was This Post Helpful? 0
  • +
  • -

#15 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 379
  • Joined: 27-December 08

Re: Need some help with char and string

Posted 08 June 2009 - 11:59 AM

View PostAmadeus, on 8 Jun, 2009 - 10:42 AM, said:

Good lord this thread is way off base :) Could the OP please restate your request? It read to me like you wanted to find a substring within a char array. If that is the case, use the strstr function as suggested above. If you want to assign a char array to a string object, you can simply assign it (the cast is implicit)
std::string str;
char array[] = "Hello World";

str = array;


If you want something else, please elaborate.

string has some diffrent encoding
char has the encoding i want actually i want to read a binary file
in an char array
string only reads ascii and
char reads every thing in it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2