How to write a C code that does the following?
Hello there,
I would like to write software that calls for two strings through the user along with checks for your common chars around string1 in addition to string2 without having repeating exactly the same char double..
as an example:
first_string:”4*3minus5is7″
second_string:”1*+-12345ijklmn”
The exact result string is:”4*3mn5i”
please HELP
// This particular works.
// Come across Common People in a pair of Strings, with out duplicates
#include
#include
#define MAX_LEN 1000
int main()
char first_stringMAX_LEN, second_stringMAX_LEN;
char common_stringMAX_LEN = “”;
char *pa, *pb, *pc;
printf(“Enter 1st string\n”);
scanf(” %\n”, first_string);
printf(“Enter Minute string\n”);
scanf(” %\n”, second_string);
int my partner and i = 0;
for(pa = first_string; *pa; pa++)
for(pb = second_string; *pb; pb++)
if(*pa == *pb)
personal computer = common_string;
while(*pc && *pc! = *pa)
pc++;
if(! *pc)
common_stringi++ = *pa;
printf(“Common String:%s”, common_string);
getch();
give back 0;
##########
When using the inputs an individual gave I acquired the expenditure “4*3min5” that is certainly correct I guess!
##########
If you consider this by way of logically, and come up with a solution in some recoverable format, or with your head, you should be able to come up that has a fairly easy algorithm to ensure your code just isn’t complicated.You might have something such as this:
#include
#include
#include
#define MAX_LINE_LEN 256
#define MAX(a, b) (((a) > (b)) (a):(b))
char lineMAX_LINE_LEN;
emptiness getString(char *prompt, char *input);
int main(int argc, char *argv)
char *s1, *s2, *sI, *p, *q, *r;
size_t s1Len, s2Len;
/* Have input plus allocate storage for C-strings */
getString(“Enter string 1:”, line);
s1 = (char *)calloc((s1Len = strlen(line)) + JUST ONE, sizeof(char));
strcpy(s1, line);
getString(“Enter string 2:”, line);
s2 = (char *)calloc((s2Len = strlen(line)) + JUST ONE, sizeof(char));
strcpy(s2, line);
ur = sI = (char *)calloc(MAX(s1Len, s2Len) + JUST ONE, sizeof(char));
/* Keep intersection regarding s1 plus s2 around sI */
regarding (p = s1; *p! = ”; ++p)
in the event that (((q = strchr(s2, *p))! = NULL) && (strchr(sI, *p) == NULL))
*r++ = *p;
printf(“Intersection:%s\n”, sI);
free(s1);
free(s2);
free(sI);
give back 0;
emptiness getString(char *prompt, char *input)
printf(“%s”, prompt);
fgets(input, MAX_LINE_LEN, stdin);
*(strchr(input, ‘\n’)) = ”;
#if 0
Sample run:
Enter in string 1:4*3minus5is7
Enter in string 2:1*+-12345ijklmn
Intersection:4*3min5
#endif
Start by means of writing a thought.The idea I take it becomes to iterate by way of first_string.length — 1 tugging out 1 character at the moment and comparing considering the same index while in the second stringed.Create the exact result string made by this.
Leave a Reply
You must be logged in to post a comment.