473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Put tags infront and back of the string....

agsrinivasan
hi everyone...

I had done the program to search a string in a text file...Now i want to put tags,,like $$ into infornt of as well as after end of searched string

Herewith i had attached the code...

I had little confusion with this task...

Here is my program...
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4.  
  5.  
  6. int main (void) {
  7.  
  8.     FILE *Input;
  9.  
  10.     long lSize,StringId;
  11.  
  12.          size_t result;
  13.  
  14.         char *buffer;
  15.  
  16.         Input = fopen("/home/srinivas/Desktop/new.txt","r+");
  17.     printf("Input value: %d\n",Input);
  18.  
  19.     if (Input ==NULL) {fputs ("File error",stderr); exit (1);}
  20.  
  21.     fseek (Input ,0 , SEEK_END);
  22.  
  23.     lSize = ftell (Input); printf(" The file size is = %ld\n",lSize);
  24.  
  25.         rewind (Input);
  26.  
  27.       // allocate memory to contain the whole file:
  28.       buffer = (char*) malloc (sizeof(char)*lSize);
  29.       if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
  30.  
  31.       // copy the file into the buffer:
  32.       result = fread (buffer,1,lSize,Input);
  33.  
  34.        long i,j,len,z,no=0,x = 0;
  35.  
  36.           char string1[20];
  37.  
  38.           printf(" Enter the Name you want to search \n");
  39.  
  40.            gets(string1);
  41.  
  42.           len = strlen(string1);
  43.  
  44.           //printf("The length of the string = %d ",len);
  45.  
  46.            char string2[20];
  47.  
  48.  
  49.         //  char string3[20],ch;
  50.  
  51.             /* int g = 0;
  52.  
  53.                 while(fgetc(Input)!=  ' ')
  54.         {
  55.          ch = fgetc(Input);
  56.  
  57.          string3[g] = ch ;
  58.  
  59.                 g++;
  60.  
  61.         }          
  62.  
  63. printf("\n \n string3 = %s ",string3); */
  64.  
  65.          // char *compare;
  66.  
  67.         char string3[50];
  68.  
  69.     fseek (Input ,0 , SEEK_SET);       
  70.  
  71.     for(i=0;i < lSize ; i++)
  72.     {
  73.           if(string1[0] == buffer[i]){
  74.  
  75.              for(z=0, j=i ;j < i+len ;j++)
  76.            {                 
  77.  
  78.                     string2[z] = buffer[j];
  79.                          z++;
  80.  
  81.  
  82.                        }
  83.  
  84.  
  85.                   if(strcmp(string1,string2) == 0)
  86.                     {
  87.                         printf("string successfully searched\n ");
  88.                              no++;
  89.                                      printf(" \t \n String 2 is = %s",string2);
  90.  
  91.                                    }                                           
  92.                          //else
  93.                                //  printf("string Not found\n");
  94.     }
  95.  }
  96.  printf("\t \n matched string is found in text file  %d times= ",no);
  97.  
  98.  //printf(" \t \n String 2 is = %s",string2);
  99.  
  100.  
  101.  
  102. fclose(Input);
  103.  
  104.  
  105.  
  106.  
  107.  
  108. }
  109.  
Feb 15 '07 #1
3 1476
Ganon11
3,652 Expert 2GB
Using char arrays, you will have to take the following steps:

1) Determine the size of the original string - possibly with strlen
2) Create a new char array of size strlen(original + 2 * (length of prefix/suffix if the same))
3) The first x characters should be set to your prefix
4) Manually copy the contents of the string to the new array
5) Add the suffix to the very end
Feb 15 '07 #2
hi,
I had tried with one more method...But it was tagging only first string...some logic Missing...

This is my code...partially modified....

......................................

Expand|Select|Wrap|Line Numbers
  1.           for(i=0;i < lSize ; i++)
  2.   {
  3.       if(string1[0] == buffer[i])
  4.          {
  5.               for(z=0, j=i ;j < i+len ;j++)
  6.            {                 
  7.                          string2[z] = buffer[j];
  8.                          z++;
  9.                      }         
  10.                        // if(strcmp(string1,string2) == 0)
  11.                  //  {
  12.                             //while(buffer != -1){    
  13.                         char *pch;    
  14.                                    pch = strstr(buffer,string2);    
  15.                         strncpy (pch-1,"$",1);
  16.                          strncpy(pch+len,"$",1);       
  17.                                         printf("\t string successfully searched\n \n  ");
  18.                              no++;
  19.                                         printf(" \t \n String 2 is = %s",string2);
  20.                                         //pch+len;
  21.                           //   }
  22.  
  23.  
  24.  
  25.  
  26. //
  27.               }
  28. }
  29.       printf("\t \n matched string is found in text file  %d times\n \n ",no);
  30.       puts(buffer);
  31.  //printf(" \t \n String 2 is = %s",string2);
  32.  
  33.    fclose(Input);
  34.  
  35. }


I got output in a bufffer....i tried a word CBE....see the output...

19. WP.4832/2007 M/S.J.KARUNANIDHI $CBE$ (POLLUTION) S. HEMANANTH
FOR DISP WITH
MP.1/2007
FOR STAY
MP.2/2007

20. WP.4834/2007 M/S.J.KARUNANIDHI CBE
(POLLUTION) S. HEMANANTH
For Dispense with
MP.1/2007
For Stay
MP.2/2007

21. WP.4836/2007 M/S.J.KARUNANIDHI CBE
(POLLUTION) S. HEMANANTH
To dispense with
MP.1/2007
For Stay
MP.2/2007

22. WP.4842/2007 MR.V.KALYANARAMAN CBE
(POLLUTION)
For Direction
MP.1/2007


first time only getting tagged..

any suggestions....
Feb 16 '07 #3
Using char arrays, you will have to take the following steps:

1) Determine the size of the original string - possibly with strlen
2) Create a new char array of size strlen(original + 2 * (length of prefix/suffix if the same))
3) The first x characters should be set to your prefix
4) Manually copy the contents of the string to the new array
5) Add the suffix to the very end

Thanks a lot....it is working..



Regards...

srini
Feb 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mitchua | last post by:
I am trying to use HTML::Parser to parse an HTML file, remove all HTML tags (including comments, etc.), replace all ENTITIES (e.g. &amp), and put the result into a variable as a string. I figure...
15
by: Jeff North | last post by:
Hi, I'm using a control called HTMLArea which allows a person to enter text and converts the format instructions to html tags. Most of my users know nothing about html so this is perfect for my...
8
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
2
by: Got2Go | last post by:
Hello, I am trying to come up with a function that will search the source of the page for a specific string. If that string is found, then have it modify specific tags inside of a table and...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
1
by: Scanner2001 | last post by:
I am looking for a way to return the output of a web service as a string without the xml tags, just the raw data. I am calling the web service from an html page using a form post method....
10
by: Ann Huxtable | last post by:
Why is it that sometimes strings used in C# are pre-pended by an '@' character?. Is there any documentation on this (Google searches don't bring anything useful - "c# + '@' + string variables" etc )
6
by: ehabaziz2001 | last post by:
My Program turned a char variable into string using null character but unexpected character displayed infront of the variable after using printf . Syntax : ------------- #include <stdio.h> ...
2
by: Joah Senegal | last post by:
Hello all, I;m a beginner with XML. All I want is to print the XML string with tags. In the following example, there is function called: xmlNodeGetString. This function is getting a char* back....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.