if i want to set a string from the second index in an array until a '\0' character how would i do that?
i can't str_cpy() can i?
Printable View
if i want to set a string from the second index in an array until a '\0' character how would i do that?
i can't str_cpy() can i?
for-loop. starts at the required index value. terminates when the char being examined is equal to '\0'. inside the loop you assign the char from the array to the proper index position in your string, e.g. mystring[i-1] = arr[i];
Maybe a while loop:
Code:int I = 1;
while( charArray[I] != '\0' )
{
theString += charArray[I];
++I;
}
Yes, you can use strcpy. Assuming you are using C style strings, just use strcpy(target, source + 1) or replace 1 with whatever index you want to start with.
i just used a while loop. i figured that was probably the fastest way, but it seems kind of slow. hopefully its not as i use it a lot. thank you for the help.
If you implemented it correctly it shouldn't be any slower than using strcpy, but why not use the built-in function anyway?
You would have to include the whole <cstring> header file?
If you're really concerned about speed, and you don't mind pointer arithmetic, behold the fastest way:
Code:#include <iostream>
int main()
{
char foo[] = "BlahThis is a test\0";
cout <<(foo + 4) <<endl; //Outputs "This is a test"
}
Do you really need the '\0'? I thought the compiler automatically inserted the null terminator to the end of string literals.