![]() |
| | #1 |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| The code goes trough the string using it's lenght in a for loop, while it goes trough, it starts finding the letters from the first item and also increments j, to look for the other letter and if it is not found, it resets it, then if j is equal to the first item's lenght, it continues with the second one and it does the same, but if it is equal to the second item lenght it returns the string in between Code: char *getFromMiddle(char *string, int l_string, char *first, int l_first, char *second, int l_second) { // the function has to return a char pointer, is it well declared?
int i, j, x, f_pos, s_pos, l_middle;
char t, *middle;
printf("Declara variables \n");
for(i=0;i<l_string;i++) {
printf("For numero: %i \n", i);
if(x != 1) {
if(*string[i] == *first[j]) { //trouble here
printf("Igualdad entre i nš=%i(%c) con j nš=%i(%c): %i \n", i, string[i], j, first[j]);
j++;
if(j == l_first) {
printf("J(%i) = l_first(%i)",j ,i);
f_pos = i;
j = 0;
x = 1;
}
}
else {
printf("No se encontro igualdad en i->%i(%c) == j->%i(%c)",i ,string[i], j, first[j]);
j = 0;
}
}
else {
if(*string[i] == *second[j]) { // also here
j++;
if(j == l_second) {
s_pos = i-l_second;
l_middle = s_pos-f_pos;
middle = malloc(sizeof(t)*(l_middle+1));
for(x=0;x<l_middle;x++) {
*middle[x] == *string[f_pos];
f_pos++;
}
*middle[l_middle+1] = '\0';
return middle; // is it middle well returned?
}
}
else {
j = 0;
}
}
}
return 0; // return 0 if nothing was found
}
Thanks Last edited by lautarox; 05-13-2009 at 12:12 PM. |
| lautarox is offline | |
| | #2 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: if(*string[i] == *first[j]) { //trouble here
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 05-13-2009 at 12:35 PM. | ||
| MK27 is offline | |
| | #3 | |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| Quote:
I'm triying to make a function, to get a string from the middle of two items, like <hello>asas</hello>, the function should get asas. I mean like a "classic" -> normal char array, is it possible to use the pointer with the [] tags as the char array? By the way, thanks for your time Last edited by lautarox; 05-13-2009 at 12:43 PM. | |
| lautarox is offline | |
| | #4 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
| |
| itCbitC is offline | |
| | #5 | |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| Quote:
I'll try using strtok() Edit*, I won't be able to use strtok(), because of the large ammount of tags in the string.. Last edited by lautarox; 05-13-2009 at 01:08 PM. | |
| lautarox is offline | |
| | #6 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: #include <stdio.h>
#include <string.h>
int main() {
char string[]="<h1>Example</h1>",*tok;
tok=strtok(string,"<>");
printf("%s\n",tok);
while ((tok=strtok(NULL,"<>"))) printf("%s\n",tok);
printf("Now string=%s\n",string);
return 0;
}
That may or may not be useful to you, depending on the context. Your original idea is not a bad one, you just need to work it out more slowly and always try and keep it so that you can compile and test (with printf for debugging) every couple of lines -- even if this means making small simplifications that are not permanent.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #7 |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| Thanks MK27, but I think that strtok() won't work because of the large amount of tags the string has, could you tell me how could I access the value of a character from the pointer string using the [] tags? that's what I need to continue debugging the code. Example: // ptr is a pointer, and so it is ptr2 I want it to point to the (1) character, it would be the second counting from the 0, so it would compare the value they have. if(ptr[1] == ptr2[1]) { ... Last edited by lautarox; 05-13-2009 at 01:36 PM. |
| lautarox is offline | |
| | #8 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: #include <stdio.h>
#include <string.h>
void tagsout (char *ptr) {
int len = strlen(ptr), i, j=0, tag=0; /* "tag" is a flag */
char tmp[len];
for (i=0; i<len; i++) {
if (ptr[i]=='<') tag=1;
if (ptr[i]=='>') { tag=0; continue; }
if (tag) continue; /* flag is set, so skip */
tmp[j]=ptr[i]; j++; /* or add to tmp and advance j counter */
}
tmp[j]='\0'; /* null terminate replacement */
strcpy(ptr,tmp);
}
int main() {
char string[]="<div class=\"this\"><p>Hello</p><center><h1>Example</h1></center></div>";
tagsout(string);
printf("%s\n",string);
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #9 |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| lol, I don't know why I used the *'s , It worked almos perfect without the *'s, now I'm having a problem here.. Code: if(j == l_second) {
s_pos = i-l_second;
l_middle = s_pos-f_pos;
printf("J(%i) = l_second(%i) s_pos = %i , f_pos = %i , l_middle = %i \n",j ,i, s_pos, f_pos, l_middle);
int test;
test = sizeof(t)*(l_middle+1);
printf("test = %i \n", test);
middle = malloc((sizeof(t))*(l_middle+1));
if(middle == NULL) {
printf("No se alloco \n");
exit(1);
}
for(x=0;x<l_middle;x++) {
middle[x] == string[f_pos];
f_pos++;
}
middle[l_middle+1] = '\0';
printf("Termina middle = %s \n", middle);
return middle;
}
test = 7 Termina middle = -> why middle is not beeing printed? |
| lautarox is offline | |
| | #10 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Instead of comparing for equality shouldn't this be an assignment with a single equals sign, as in Code: middle[x] = string[f_pos]; |
| itCbitC is offline | |
| | #11 |
| C/C++ Learner & Lover Join Date: Aug 2008 Location: Argentina
Posts: 172
| lol, thanks, it worked perfectly! |
| lautarox is offline | |
| | #12 |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| second solution, it does for specific tags only |
| c.user is offline | |
![]() |
| Tags |
| array, char, function, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another syntax error | caldeira | C Programming | 31 | 09-05-2008 01:01 AM |
| get keyboard and mouse events | ratte | Linux Programming | 10 | 11-17-2007 05:42 PM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| String sorthing, file opening and saving. | j0hnb | C Programming | 9 | 01-23-2003 01:18 AM |