Im a newbie in programming.
How do you retreive part of a string in a c program?
eg.
char mytext[] = "abcdef[12345]";
I want to retreive the value inside the []...
Please help.
Im a newbie in programming.
How do you retreive part of a string in a c program?
eg.
char mytext[] = "abcdef[12345]";
I want to retreive the value inside the []...
Please help.
mytext[0] will refer to the first character in the string, and so on... you can use functions in string.h such as strcpy, strcmp and strstr to manipulate and look through them.
http://www.cplusplus.com/ref/cstring/
you might find that link handy.
.sect signature
it still sounds difficult to me.
I tried, but it doesnt work.... 8(
line[]="12345";
lineLen = strlen(line);
printf("%s\n", line);
pos = 1;
while (pos != lineLen)
{
strcat(output, line[pos]);
pos++;
}
I have decalre output
#define MAXSIZE 30000
...
...
char output[MAXSIZE];
aww....
anyone show me a good function to retreive a part of a string.
It depends on what you need to retrieve and how variable it is. If it's the same thing with every string then the strstr() function is ideal, but if it's in between two delimiting characters you'll have a harder time of things. The formatting of the string is also important and it must remain constant if you want your code to work consistently.anyone show me a good function to retreive a part of a string.
Code:#include <stdio.h> #include <string.h> #define OPEN '[' #define CLOSE ']' int main (void) { char mytext[] = "abcdef[12345]"; char *Start = strchr (mytext, OPEN); if (Start){ while (*Start++){ if (*Start == CLOSE) break; /* Do what you need to with the data, save it to another string or process it as is. This part is up to you. */ } } return 0; }
cool
thanks dude
And just for fun, here's another version of the same theme...
This uses 2 strchr() calls to find the start and end markers, and then determines the distance between them. And finally, it prints the proof that it's worked.Code:#include <stdio.h> #include <string.h> #define OPEN '[' #define CLOSE ']' int main(void) { char mytext[] = "abcdef[12345]"; char *Start; char *End; if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL) { /* Now Start points to the [ and End points to the ] */ /* Here's the proof: */ char buf[BUFSIZ]; int Len = End - Start + 1; strncpy(buf, Start, Len); buf[Len] = '\0'; printf("Buf contains: %s\n", buf); } return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
No offense Hammer, but I have a little bit of a problem with this line. Nothing serious, it just does too much for one statement and can be confusing to a reader. I feel, and this is just me, that it would be better if the line were broken up into several logical lines.Code:if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
Code:Start = strchr (mytext, OPEN); End = strchr (Start, CLOSE); if (Start && End)
No offense taken, I've been around too long to get annoyed at people disliking my codeOriginally posted by Crimpy
No offense Hammer, but I have a little bit of a problem with this line. Nothing serious, it just does too much for one statement and can be confusing to a reader. I feel, and this is just me, that it would be better if the line were broken up into several logical lines.Code:if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
Code:Start = strchr (mytext, OPEN); End = strchr (Start, CLOSE); if (Start && End)
Anyway, yes, I can understand that line is longer than average for a newbie, but I don't believe an experienced programmer would have any problem understanding it.
Your suggested correction fails to take into account the fact that the first call the strchr() might not find an opening marker ([ in this case), and therefore the program will crash and burn on the second call, with a NULL pointer being passed to it.
There are of course, many ways in which you could rearrange these statements to make them more friendly to the newbie, and one of the simplest is this:
This is still using the same single if statement, but being over 2 lines, it aids reading.Code:if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
And of course, a comment or two wouldn't go a miss in a real prog
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Just another variation on the same theme...
there is one (minor) flaw here: this code does not check whether the "]"-character really exists in the input string. "%[^]]" stops reading characters if a "]"-character is found, OR if the end of the string is reached.Code:#include <stdio.h> int main(void) { char mytext[] = "abcdef[12345]"; char number[20]; int test; /* sscanf: throw away all up to "["; read "["; and then read characters up to "]" */ test = sscanf(mytext, "%*[^[]" "[" "%[^]]", number); if(test==1) /* sscanf should have read read exactly one field */ { printf("The answer is: %s\n", number); } return 0; }
alex