Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LAB 4B To gain experience in using arrays, strings, and functions. Plan, code, a

ID: 3704608 • Letter: L

Question

LAB 4B To gain experience in using arrays, strings, and functions. Plan, code, and execute a C program to a) code a function to generate a string, sl, containing a random pattern of upper case letters 'A'-Z. r b) code a function to allow the user to enter a string, s2, (minimum length 2 characters, maximum Problem definition: 21 Jength is 20 cha ) containing a random pattern of upper case c) code a user defined function, sufilterlsl,s2,c) that searches sl and replaces any occurrences of any d) output the generated string, the 'user entered' string, and the filtered string from the main program. letters A -Z and then a replacement character, caut of the characters contained in s2 with the character c TEKILSTOE VAALK,VOTUPAWENEKLALKYUNCIOCAPVINOAS s2-CAL filtered s i-(TJK.SI OEFN . . . KVOUP" wENFK.. . . KYUFGIOE. PVNO. ? Allow the user to enter imuliple strings, (s2.if desired. Use the originalandom string for all comparisons To create th from 0-32767. The modulus operator can be used to change the range to 0-25, This should be done in a function. Generate the random string-only once rand d pattern use generates Remember: STRINGS MUSTTERMINATE WITdw] DO NOT USE POINTER NOTATION OR GLOBALS IN THIS LAB (4B2 LAB 5B To gain experience in the use of pointers. fodify lab 4 by replacing all array notation with pointers notation. Howeverarrays.must still be defined. No subscripts in functions, USE PURE POINTER NOTATION

Explanation / Answer

/* Lab 4B *-To gain the experience in using array, string & functions */

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

/* function to generate
void genstr(char s1[])
{ int i=0;
while(i!=40)
{ /*Generate random number 0-25 and ascii code of A-Z is 65-90 so 65 is added to get upper case */
s1[i]=(rand()%25)+65;
i++;
}
s1[i]='';
}

char inpstr(char s2[],char c)
{ int i=0;
char a;

/* 10 is ascii value of new line character for end of input*/
do
{ a = getchar();
if(a>='A' && a<='Z' || a==10) /* accept only upper case character or new line */
{
if(a!=10 && i!=20) /*check for new line character & input limit of 20 character */
{ s2[i] = a;
   i++;
}
else
{
   if(i>=2) /* Check to input minimum 2 characters */
   s2[i] = '';
   else
   {a=' ';continue;}
}
}
else
continue;
}while(a!=10);

puts("Replace with:");
c=getchar();

return(c);
}

void strfilter(char s1[],char s2[],char c)
{ int i=0,j;
while(s2[i]!='')
{ j=i;
while(s1[j]!='')
{
if(s1[j]==s2[i])
{s1[j]=c;}
j++;
}
i++;
}
}

int main()
{
char s1[41],s2[21],c;
puts("Random generated string:");
genstr(s1); /* calling function to generate random number */

puts(s1);
puts("Enter the string in upper case letters 'A'-'Z' to be replaced[2-20 character] and replaement character:");
c=inpstr(s2,c); /* Calling function to input string and character replaced */
strfilter(s1,s2,c); /* calling function to replace the character */
puts("User Entered String:");
puts(s2);
puts(" Filtered String:");
puts(s1);
return(1);
}

/* Lab 5B using pointers*/

#include <stdio.h>

#include <stdlib.h>

void genstr(char *s1)

{ int i=0;

while(i!=40)

{

*(s1+i)=(rand()%25)+65;

i++;

}

*(s1+i)='';

}

void inpstr(char *s2,char *c)
{ int i=0;

char a;

do
{ a = getchar();
if(a>='A' && a<='Z' || a==10) /* accept only upper case character or new line */
{
if(a!=10 && i!=20) /*check for new line character & input limit of 20 character */
{ *(s+i) = a;
   i++;
}
else
{
   if(i>=2) /* Check to input minimum 2 characters */
   *(s+i) = '';
   else
   {a=' ';continue;}
}
}
else
continue;
}while(a!=10);

puts("Replace with:");

*c=getchar();

}

void strfilter(char *s1,char *s2,char c)

{ int i=0,j;

while(*(s2+i)!='')

{ j=i;

while(*(s1+j)!='')

{

if(*(s1+j)==*(s2+i))

{*(s1+j)=c;}

j++;

}

i++;

}

}

int main()

{

char *s1,*s2,c;

puts("Random generated string:");

genstr(s1);

puts(s1);

puts("Enter the string in upper case letters 'A'-'Z' to be replaced[2-20 character] and replaement character:");

inpstr(s2,&c);

strfilter(s1,s2,c);

puts("User Entered String:");

puts(s2);

printf(" Filtered String:");

puts(s1);

return(1);

}