Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with search and replace 1

Status
Not open for further replies.

FredrikN

Programmer
Jan 5, 2001
60
0
0
SE
Hi
I'm a perl programer but I want to try out some C now.

Look at this simple exampel in perl
It will replace all h with H

$tmp = "hello out there";
$tmp = ~s/h/H/g;

output: Hello out tHere


How can I do this in C. I just searched the forum but I can't find anything about this.

Is the problem hard to solve i C ??


Thanks

//FredrikN
 
Do something like this to go through the string looking for the character you want to replace:
Code:
char sString[512];
strcpy(sString, "hello out there");

printf("%s\n\n", sString);

for (int i = 0; i < strlen(sString); i++)
{
  if (sString[i] == 'h')
  {
    sString[i] = 'H';
  }
}

printf(&quot;%s\n\n&quot;, sString);
 
Hi,

When you have that down, you may want to put it into a function that is passed the string to fix and the character you want to change the case for. You can then use a standard function like toupper() and some string functions like strchr() within the function to change the case and to get some practice using them. They are almost essential for string manipulation unless you want to rewrite every single one of them.

The above example is fine, but it's just too specific to be of any use for other letters. (Don't worry - I know programsecrets is keeping it simple.)

You will find many other things that are much easier to do in Perl than in C especially string manipulation, but you will learn more &quot;how&quot; things are done behind the scenes learning C.

-Tyler
 
Thanks , almost done :)
Only one problem.

Let's say I want to replace the a with something like aaa-AAA

The compiler complains about the aaa-AAA the it only can contain 1 character.


if (sString == 'a')
{
sString = 'aaa-AAA';
}


Would be great if someone can help me out with this problem


error code :

program.c53:character constant too long
program.c53:warning:eek:werflow in implicit constant vonversion
 
Ok,try this:

char Buff[1024];
char String[] = &quot;how are you doing today ?&quot;;
char rString[] = &quot;aaa-AAA&quot;;
int k = 0;

printf(&quot;%s\n\n&quot;, String);

for ( int i = 0; String != '\0'; i++ )
{
if (String == 'a')
{
for ( int j = 0; rString[j] != '\0'; j++ )
{
Buff[k] = rString[j];
k++;
}
}
else
{
Buff[k] = String;
k++;
}
}
Buff[k] = '\0';

printf(&quot;%s\n\n&quot;, Buff);

the above code will replace all 'a' caracter by the string &quot;aaa-AAA&quot;.
 
THat looks fine. However, if you reverse the problem statement i.e. replace aaa-AAA
with &quot;a&quot; then the things become a bit different. In that case, the origin string needs to be parsed until the sequence of characters is obtain and then replaced. This can be done by designing the code to flow from one state to another and can be implemented easily through the &quot;case&quot; condotional structure.

This will give us the most generic function to replace one word with other.

This method is used by compilers to identify the commands.
 
This was very interesting indeed as it was more difficult to implement.
But after a few try, i have succeeded.
I didn't use the &quot;case&quot; condotional structure.

Here is the result:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *ReplaceString( char *buff, char *oldstring, char *newstring )
{
char *bkp = (char*)malloc( sizeof(char) * strlen( buff ) * (strlen(oldstring) + 1));
char *tmp = (char*)malloc( sizeof(char) * strlen( buff ) * (strlen(newstring) + 1));

if( !bkp || !tmp )
{
fputs( &quot;memory allocation has fail.\n&quot;, stderr );
return (NULL);
}

const int max_size = 10000;

if( strlen( tmp ) > max_size )
{
fputs(&quot;not enough space in the buffer \&quot;newbuff\&quot;\n&quot;, stderr);
return (NULL);
}

char newbuff[max_size];
unsigned int len = strlen(oldstring);
int pos;
char *pdest;
int i = 0;

strcpy( bkp, buff );
tmp[0] = 0;
newbuff[0] = 0;

while( buff != 0 )
{
pdest = strstr( buff, oldstring );
pos = pdest - buff + 1;

while( pdest != NULL )
{
bkp[pos - 1] = 0;
strcat( bkp, newstring );
strcat( tmp, bkp );
buff += pos + len - 1;
strcpy( bkp, buff );

pdest = strstr( buff, oldstring );
pos = pdest - buff + 1;
}

i++;
}

strcat( tmp, buff );
strcat( newbuff, tmp );

free( bkp );
free( tmp );


return newbuff;
}

void main()
{
char *String = &quot;hi how aaa-AAAre you doing todaaa-AAAy ?&quot;;
char *oldstring = &quot;aaa-AAA&quot;;
char *newstring = &quot;a&quot;;
char *temp;

temp = ReplaceString( String, oldstring, newstring );

if( temp != NULL )
{
printf(&quot;String = %s\n&quot;, String);
printf(&quot;String = %s\n&quot;, temp );
}
}
 
sorry, i meant that i didn't use the &quot;case conditional structure&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top