| | |
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:
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.
>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:
c Syntax (Toggle Plain Text)
char insert[] = "abcdefgh";
Last edited by Narue; Feb 1st, 2008 at 5:05 pm.
I'm here to prove you wrong.
•
•
•
•
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";
}
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:
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.

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)
#include <iostream> int main(int argc, char**argv) { char *insert = "abcdefg"; for (int i = 0; i < 7; ++i) std::cout<<insert[i]<<"\n"; }
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.
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.
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.
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
But i can't imagine what you only can do with array of char. Please answer me.
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { 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]; } return 0; }
But i can't imagine what you only can do with array of char. Please answer me.
![]() |
Similar Threads
- fstream Tutorial (C++)
- splitting a string and then converting (C#)
- Splitting a string? (C)
- Having problems saving objects into an array, then accessing the data later for repo (Java)
- Help! What Language is this? (Assembly)
- Debugging string function (C++)
- This ought to be simple - extra spaces (PHP)
- File parsing in 'C' (C)
Other Threads in the C Forum
- Previous Thread: Examples of C programming
- Next Thread: Call-By-Value versus Call-By-Refference
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






