C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-13-2009, 12:06 PM   #1
C/C++ Learner & Lover
 
Join Date: Aug 2008
Location: Argentina
Posts: 172
Question Pointers & char arrays

I'm triying to make a function to get something from the middle of another 2 strings, this is what I've got..
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
}
I'm having troubles with the pointers and the char arrays, how can I use them as a classic char array? Also I don't really know if the return variable will be a char pointer..
Thanks

Last edited by lautarox; 05-13-2009 at 12:12 PM.
lautarox is offline   Reply With Quote
Old 05-13-2009, 12:24 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by lautarox View Post
I'm having troubles with the pointers and the char arrays, how can I use them as a classic char array? Also I don't really know if the return variable will be a char pointer..
Thanks
What do you mean by "classic char array"? And why are you dereferencing everywhere:
Code:
if(*string[i] == *first[j]) { //trouble here
English is not your first language so I will let you know: this sentence makes no sense, so it's hard to decide what your intent is:
Quote:
I'm triying to make a function to get something from the middle of another 2 strings
You never set j (or x) to 0, either, meaning they could be anything from the start. I would say this code obviously does not work at all, and you are making a big mistake writing so much without *any* of it working. It is a *very bad idea* to just write a huge block like this presuming it will all work before you try and compile even once. You should break the task down into smaller steps so that you can write something simple, make sure it works the way you intend, and then get more complicated.
__________________

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   Reply With Quote
Old 05-13-2009, 12:40 PM   #3
C/C++ Learner & Lover
 
Join Date: Aug 2008
Location: Argentina
Posts: 172
Quote:
English is not your first language so I will let you know: this sentence makes no sense, so it's hard to decide what your intent is:
You are right =P
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   Reply With Quote
Old 05-13-2009, 12:48 PM   #4
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Quote:
Originally Posted by lautarox View Post
You are right =P
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
Yep! it is possible to use [] as an alternative to the pointer "->" notation, though when there's structured HTML text like this one strtok() is a better choice in picking out pieces of the line based on the "<" or ">" delimiters.
itCbitC is offline   Reply With Quote
Old 05-13-2009, 12:51 PM   #5
C/C++ Learner & Lover
 
Join Date: Aug 2008
Location: Argentina
Posts: 172
Quote:
Yep! it is possible to use [] as an alternative to the pointer "->" notation
How could it be used? an example will be appreciated.
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   Reply With Quote
Old 05-13-2009, 01:08 PM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by lautarox View Post
How could it be used? an example will be appreciated.
I'll try using strtok()
Here's a short example that might help if you also read some documentation:
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;	
}
Notice that the *first* strtok() uses "string", and after that NULL since this is already set. Also be aware that this does strange things to "string" itself, which is shown with the last printf.

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   Reply With Quote
Old 05-13-2009, 01:15 PM   #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   Reply With Quote
Old 05-13-2009, 01:35 PM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by lautarox View Post
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
strtok() will work on a longer string too, but here's something that demonstrates the proper syntax for passing a C string and accessing it's individual elements (characters):
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;	
}
Hopefully that will at least get you started...
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 05-13-2009, 02:04 PM   #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;
					}
It returns:
test = 7
Termina middle = -> why middle is not beeing printed?
lautarox is offline   Reply With Quote
Old 05-13-2009, 02:11 PM   #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   Reply With Quote
Old 05-13-2009, 02:25 PM   #11
C/C++ Learner & Lover
 
Join Date: Aug 2008
Location: Argentina
Posts: 172
lol, thanks, it worked perfectly!
lautarox is offline   Reply With Quote
Old 05-13-2009, 08:49 PM   #12
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
second solution, it does for specific tags only
Attached Files
File Type: c clear_tags.c (1.0 KB, 14 views)
c.user is offline   Reply With Quote
Reply

Tags
array, char, function, pointer

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22