C Programming - function to return string?

Anvorn

New member
For my lab, I have to code a user-defined function named strfilter(s1, s2, c). Basically, it will take s1 and replace the elements listed in s2 with a particular character, 'c'. Now, the trick is, I have to return a string or at least point to a new string without modifying s1 - all three parameters have to be preserved. Everything seems to work alright, I just need to pass the value/string/pointer, and I'm having a hell of a time doing it. I know this is not efficient at all, but I have certain restrictions in how I code this (such as string manipulation cannot be done by pointer notation).

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

#define BUF_SIZE 512
#define GENERATE_STRING 40

char *randomStr ( char randomString[] );
char strfilter( char s1array [], char s2array[], char rplcmnt );

int main(void)
{
char s1[GENERATE_STRING];
char s2[BUF_SIZE];
char *filtered[GENERATE_STRING];
char replace;

randomStr( s1 );
printf( "Enter a line of a random patter of upper case letters A - Z: \n" );
gets( s2 );
printf( "Enter a replacement character: \n" );
scanf( "%c", &replace);
fflush(stdin);
printf( "%s\n", s1 );
printf( "%s\n", s2 );
printf( "%c\n", replace );
printf( "%s\n", (strfilter( s1, s2, '*' )) );
printf( "%s\n", filtered );
return 0;
} //main

char *randomStr ( char randomString[] )
{
int randIndex = 0;
randomString[GENERATE_STRING] = '{rss:Content}';
while ( randomString[randIndex] != '{rss:Content}' )
{
randomString[randIndex] = (rand()%26) + 'A';
randIndex++;
}
return( randomString );
}

/*

*/
char strfilter( char s1array[], char s2array[], char rplcmnt )
{
char filteredArray[GENERATE_STRING];
int indexCount = 0;
filteredArray[GENERATE_STRING] = '{rss:Content}';
while ( s1array[indexCount] != '{rss:Content}' )
{
int userIndex = 0;
while (s2array[userIndex] != '{rss:Content}')
{
if ( s1array[indexCount] == s2array[userIndex] )
{
filteredArray[indexCount] = rplcmnt;
break;
}
userIndex++;
}
if (filteredArray[indexCount] != rplcmnt )
filteredArray[indexCount] = s1array[indexCount];
indexCount++;
}
return( filteredArray );
}
 
Back
Top