0

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.

Wilson
  • 1
  • 1

1 Answers1

0

Two problems, you didn't include stdlib.h where there is the declaration of the function malloc().

Also you are passing a wrong parameter to the strlen() but you passed char**. Same in other functions too. You had parameter mismatch.

char *string1[30+1];
char *string2[30+1];

You wanted

char string1[30+1];
char string2[30+1];

Also you should use strcmp when you are comparing "-i" with argv[1].

Now argv is basically array of char*. And you are basically comparing a null terminated char array with (string) with "-i". (Not '-i').

Also notice one interesting thing,

compiler says

jdoodle.c:29:15: warning: comparison between pointer and integer
     if(argv[1]=='-i'){
               ^~

An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.

'-i' is a multicharacter literal. This has type int that's why the warning comparison between pointer and int.

Also there is another thing you should clearly know, when you pass an array to a function it decays into pointer to the first element of the array.

char *s[100] decays to char** because the content of the array is char* a pointer to it would be char**.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • 1
    Thanks for the quick reply and the clear explanation. Implemented your advice and everything now works. – Wilson Dec 05 '17 at 03:49