Split string into char array

Reply

Join Date: Jan 2008
Posts: 3
Reputation: vtskillz is an unknown quantity at this point 
Solved Threads: 0
vtskillz vtskillz is offline Offline
Newbie Poster

Split string into char array

 
0
  #1
Feb 1st, 2008
Hi, I was wonder if someone could give me some help with my program

I have a variable:

char *insert = "abcdefgh"

and i want to split that into chars like so, 'a', 'b', 'c', 'd', etc.

I've tried ToCharArray(), but that doesnt seem to work
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Split string into char array

 
0
  #2
Feb 1st, 2008
>I have a variable:
>char *insert = "abcdefgh"
>and i want to split that into chars like so, 'a', 'b', 'c', 'd', etc.
Uh, you're done. A string literal is an array of const char and except for modification, sizeof, and address-of you can use the pointer as if it were an array. If you need an actual array, you can do this:
  1. char insert[] = "abcdefgh";
C doesn't have a special string type like Java, so when you're using a string in C, you're really using an array.
Last edited by Narue; Feb 1st, 2008 at 5:05 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 346
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 44
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: Split string into char array

 
0
  #3
Feb 1st, 2008
Originally Posted by vtskillz View Post
Hi, I was wonder if someone could give me some help with my program

I have a variable:

char *insert = "abcdefgh"

and i want to split that into chars like so, 'a', 'b', 'c', 'd', etc.

I've tried ToCharArray(), but that doesnt seem to work


a string in c/c++ is a array of characters so extract then like so

#include <iostream>

int main(int argc, char**argv)
{
char *insert = "abcdefg";


for (int i = 0; i < 7; ++i)
std::cout<<insert[i]<<"\n";

}
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Split string into char array

 
0
  #4
Feb 1st, 2008
This is C forum. Not C/C++
Take the time to read how properly tag your code so instead of looking like this:
#include <iostream>

int main(int argc, char**argv)
{
char *insert = "abcdefg";


for (int i = 0; i < 7; ++i)
std::cout<<insert[i]<<"\n";

}

It would look like this:
  1. #include <iostream>
  2.  
  3. int main(int argc, char**argv)
  4. {
  5. char *insert = "abcdefg";
  6.  
  7.  
  8. for (int i = 0; i < 7; ++i)
  9. std::cout<<insert[i]<<"\n";
  10.  
  11. }

For how to do that read here and here
I don't promise that it'll fix your code, but it'll make it more readable.
Last edited by Aia; Feb 1st, 2008 at 11:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: naej is an unknown quantity at this point 
Solved Threads: 0
naej naej is offline Offline
Newbie Poster
 
0
  #5
Oct 11th, 2009
hmm try this
but i'm not sure i'm just a first yr student

char a
char b
.
.
.
char g

hee hee

we're not finished discussing array yet

hmm
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
0
  #6
Oct 11th, 2009
>but i'm not sure i'm just a first yr student
If you're not sure, don't answer because you're more likely to lead people astray with bad advice than do any good.

>hee hee
Oh, I get it. You're joking. Because anybody who would jump into a thread over a year after it unofficially ended and offer something as inane as your "help" is either trying to get a laugh or is a complete retard. I don't think you're a complete retard (yet), so it must be a pitiful attempt at humor.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: Arcaiz is an unknown quantity at this point 
Solved Threads: 1
Arcaiz's Avatar
Arcaiz Arcaiz is offline Offline
Newbie Poster
 
0
  #7
Oct 11th, 2009
If you really want to make an array of char without '\0' at the end of it. You could make something like this

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. char *insert = "abcdefgh";
  6. char outsert[8]; //I use 8 because abcdefgh contains 8 char
  7. int i;
  8.  
  9. for (i=0;i<8;i++)
  10. {
  11. outsert[i] = insert[i];
  12. }
  13.  
  14. return 0;
  15. }

But i can't imagine what you only can do with array of char. Please answer me.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: harishug is an unknown quantity at this point 
Solved Threads: 0
harishug harishug is offline Offline
Newbie Poster
 
0
  #8
Oct 14th, 2009
char *insert = "abcdefgh";

char outsert[8]; //I use 8 because abcdefgh contains 8 char
int i;
for (i=0;i<8;i++)
{
outsert[i] = insert[i];
}
in array outsert you will found array of character .......
Last edited by harishug; Oct 14th, 2009 at 2:36 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC