I'm trying to write a program in C to merge 2 string together in two diffrent ways based on the given command line argument.
'-i' to merge the string alternating, like this
combine_strings –i Please enter a string of maximum 30 characters: abcde Please enter a string of maximum 30 characters: 1234567 The combined string is: a1b2c3d4e567
'-w' to concate the 2 strings together, and insert a '*' between each character of the string, like this
combine_strings -w Please enter a string of maximum 30 characters: abcde Please enter a string of maximum 30 characters: 1234567 The combined string is: abcde*1*2*3*4*5*6*7
This is what I got so far.
#include<stdio.h>
#include<string.h>
#include<memory.h>
void alt_merge(char *string1,char *string2,char *stringfinal){
while (*string1 != '\0' && *string2 != '\0')
{
*stringfinal++= *string1++;
*stringfinal++ = *string2++;
}
while (*string1 != '\0')
*stringfinal++=*string1++;
while (*string2 != '\0')
*stringfinal++ = *string2++;
*stringfinal='\0';
}
void concate_star(char *string1,char *string2,char *stringfinal){
strcpy(stringfinal,strcat(string1,string2));
}
int main(int argc,char *argv[]){
char *string1[30+1];
char *string2[30+1];
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
if(argv[1]=='-i'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
alt_merge(string1,string2,stringfinal);
printf("The combined string is: %s",stringfinal);
}
else if(argv[1]=='-w'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
concate_star(*string1,*string2,*stringfinal);
printf("The combined string is: %s",stringfinal);
}
return 0;
}
I getting these error messages.
jdoodle.c: In function 'main':
jdoodle.c:27:23: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~
jdoodle.c:27:23: warning: incompatible implicit declaration of built-in function 'malloc'
jdoodle.c:27:23: note: include '<stdlib.h>' or provide a declaration of 'malloc'
jdoodle.c:27:37: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:27:53: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:29:17: warning: multi-character character constant [-Wmultichar]
if(argv[1]=='-i'){
^~~~
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
jdoodle.c:35:23: warning: passing argument 1 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:35:31: warning: passing argument 2 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:39:22: warning: multi-character character constant [-Wmultichar]
else if(argv[1]=='-w'){
^~~~
jdoodle.c:39:20: warning: comparison between pointer and integer
else if(argv[1]=='-w'){
^~
jdoodle.c:45:44: warning: passing argument 3 of 'concate_star' makes pointer from integer without a cast [-Wint-conversion]
concate_star(*string1,*string2,*stringfinal);
^
jdoodle.c:19:6: note: expected 'char *' but argument is of type 'char'
void concate_star(char *string1,char *string2,char *stringfinal){
^~~~~~~~~~~~
I'm new to programming, so any help and advice is appreciated.