![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 4
Rep Power: 0
![]() |
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('<a href="http://abcdefr.com/c/d/odmfndfdin000008ave/direct;wi.120;hi.600/01/" target="_blank"><img src="http://ssdfcreo.com/fed/up/asdasd58s5480000008ave/direct;wi.120;hi.600/01/"/></a>'); </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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Posts: 4
Rep Power: 0
![]() |
Can u be more clear
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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.
__________________
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 4
Rep Power: 0
![]() |
im clear at my side and written the program . but stuct at the posted point.how to replace " with \" is the problem
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jan 2006
Posts: 4
Rep Power: 0
![]() |
This is really helpful for me
thank you nindoja |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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;
} 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;
}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++. |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
The only problem with using a linked list is that one character takes 5 bytes in total.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|