Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 14th, 2006, 10:47 AM   #1
trinath
Newbie
 
Join Date: Jan 2006
Posts: 4
Rep Power: 0 trinath is on a distinguished road
String Manipulation in C

Hi all,

i have the following string as input :

"<iframe src="http://abcdef.com/asd/aaa/awerftya0480000008ave/direct;wi.120;hi.600/01?page=" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" allowtransparency="true" width="120" height="600">
<script language="JavaScript" type="text/javascript">
document.write('&lt;a href="http://abcdefr.com/c/d/odmfndfdin000008ave/direct;wi.120;hi.600/01/" target="_blank"&gt;&lt;img src="http://ssdfcreo.com/fed/up/asdasd58s5480000008ave/direct;wi.120;hi.600/01/"/&gt;&lt;/a&gt;');
</script><noscript><a href="http://asedfr.com/bbb/cdf/sdfdf222200008ave/direct;wi.120;hi.600/01/" target="_blank"><img border="0" src="http://asd.com/asdas/ssd/asdeffee000008ave/direct;wi.120;hi.600/01/" /></a></noscript></iframe>"

also some more lenghty strings i need to write then into files.

for that i need to reaplce " to \" in this string .

please help me how can i replace so using a C program.

thanks in advance,

Trinath somanchi,
Hyderabad.
trinath is offline   Reply With Quote
Old Jan 14th, 2006, 10:51 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The easiest way would be to use a linked list (similar to C++'s std::string) for the text, and alter the node pointers whenever you find a ".

As an aside, IFrames and document.write(...) are generally not the best way to go about things when creating web sites.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 14th, 2006, 11:02 AM   #3
trinath
Newbie
 
Join Date: Jan 2006
Posts: 4
Rep Power: 0 trinath is on a distinguished road
Can u be more clear
trinath is offline   Reply With Quote
Old Jan 14th, 2006, 11:08 AM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
trinath, first we aren't going to do the program for you.

Second, what are you talking about: how could this be used and what for? You have to start by trying to write the program, and then if you have a specific problem then we can help. This forum is for programmers not for those looking for programmers to do work for them.
__________________

tempest is offline   Reply With Quote
Old Jan 14th, 2006, 11:46 AM   #5
trinath
Newbie
 
Join Date: Jan 2006
Posts: 4
Rep Power: 0 trinath is on a distinguished road
im clear at my side and written the program . but stuct at the posted point.how to replace " with \" is the problem
trinath is offline   Reply With Quote
Old Jan 14th, 2006, 1:18 PM   #6
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
Here is what you can do:

Read through the input one character at a time and compare it to a double quote. If it is not a double quote write the character to a file that you have open. If it is a double quote, write \" to the file.
nindoja is offline   Reply With Quote
Old Jan 14th, 2006, 9:52 PM   #7
trinath
Newbie
 
Join Date: Jan 2006
Posts: 4
Rep Power: 0 trinath is on a distinguished road
This is really helpful for me
thank you nindoja
trinath is offline   Reply With Quote
Old Jan 15th, 2006, 7:35 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Writing to a file is not the best option if you're going to read the data straight back in again. To clarify on what I meant:

A linked list is the preferred method of getting around an array's limits - i.e. a fixed length. Using a linked list, you can increase and decrease the size as necessary. This will be a very quick run-through - for a more detailed explanation, check your book on C. It will most likely have lots of diagrams to help you understand the process.

Linked lists use pointers to function. If you don't know how they work, I suggest you check them out - they're pretty fundamental to C. Linked lists are simply a series of nodes connected together:
struct Node
{
	char data;
	Node* next;
}
This structure contains two pieces of data: a character, where you can store a single byte of data, and a pointer to the next node. By linking these together, you can create a string:
	const char my_string[] = "This is a string.";
	Node* start = NULL;
	Node* current = start;
	int index;
	
	for (index = 0; my_string[index]; index++) /* loops through my_string */
	{
		/* if current is not a null pointer, we create another Node and point to that instead */
		if (current)
		{
			current->next = malloc(sizeof(Node));
			current = current->next; /* moves us onto the next Node */
		}
		
		/* copy the data into the current Node */
		current->data = my_string[index];
		current->next = NULL;
	}
This code copies the string in my_string to a linked list pointed at by start. To print the data in the linked list, we just loop through it:
int print (Node* start)
{
	Node* current = start;
	int length = 0;
	
	/* we do this until current is a null pointer */
	while (current)
	{
		printf("%c", current->data);
		length++;
		
		current = current->next;
	}
	
	return length;
}

But what benefit does this linked list have over a standard array of characters? With everything we've seen, they seem to be more complicated. Well, let's find out.

Because of the list's pointer-based nature, it isn't limited to a specific length and order. In fact, we can now add and remove characters anywhere in the string without having to duplicate it. Let's take a look at an insertion function:
int insert (Node* start, char character, int position)
{
	Node* current = start;
	int i;
	
	/* gets us to the position we want */
	for (i = 0; i < position; i++)
	{
		if (current->next)
		{
			current = current->next;
		}
		else
		{
			/* if the string isn't that long, we return -1 as an error code */
			return -1;
		}
	}
	
	/* inserts the character */
	Node* new_node = malloc(sizeof(Node)); /* creates a new node for the character to be held in */
	new_node->data = character;
	
	/* the following two statements insert it in between current and current->next, or position and position + 1
	 * this works even if current->next == NULL */
	new_node->next = current->next;
	current->next = new_node;
	
	return i;
}

You can use this insertion function to insert the backslashes wherever you want. I'll leave it to you to work out the rest.

Enjoy. If there are any questions (and I expect some - this isn't exactly the easiest tutorial to follow), don't hesitate to ask.

As an aside, there is a class in C++ called std::string which nicely encapsulates this concept in an easy-to-use datatype. You might find it easier just to switch to C++.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 15th, 2006, 7:38 AM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
The only problem with using a linked list is that one character takes 5 bytes in total.
Polyphemus_ is offline   Reply With Quote
Old Jan 15th, 2006, 7:39 AM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Much easier than insertion by duplication though, isn't it?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC