944,110 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 57234
  • C RSS
Feb 1st, 2008
0

Split string into char array

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vtskillz is offline Offline
3 posts
since Jan 2008
Feb 1st, 2008
0

Re: Split string into char array

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 1st, 2008
0

Re: Split string into char array

Click to Expand / Collapse  Quote originally posted by vtskillz ...
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";

}
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Feb 1st, 2008
0

Re: Split string into char array

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:
CPP Syntax (Toggle Plain Text)
  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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 11th, 2009
0
Re: Split string into char array
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
naej is offline Offline
3 posts
since Oct 2009
Oct 11th, 2009
2
Re: Split string into char array
>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2009
0
Re: Split string into char array
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Arcaiz is offline Offline
12 posts
since Oct 2009
Oct 14th, 2009
0
Re: Split string into char array
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harishug is offline Offline
1 posts
since Oct 2009
Oct 1st, 2011
0
Re: Split string into char array
Click to Expand / Collapse  Quote originally posted by GuestONE ...
you can do like this: char[] values = {'a','b','c','d','e','f','g'};
then address to each value using the vector(array) like this:

values[0],values[1],etc. (a,b,etc.)
you might want to see this tutorial that has also some detailes about char array: http://goldwin-advertising.ro/index....howto&Itemid=5
Yeah they probably figured that out 3 years ago when the question was posted.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
17 Days Ago
0
Re: Split string into char array
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}

OUTPUT:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arunsolo1984 is offline Offline
8 posts
since Dec 2011
Message:
Previous Thread in C Forum Timeline: tolower function for c
Next Thread in C Forum Timeline: a question about C,C++ and C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC