-2

I am new in programming and currently learning on C. Could you please assist me on solving below's case?

An example of this will be if a user is entering "cbamike", I would like to separate it into two strings: cba and mike.

I tried below's code but it doesnt work:

#include <stdio.h>;

int main (int argc, string argv[])
{
    char* input[50] = argv[1];
    char* first[10];
    char* second[10];
    sprintf(first, "%c %c %c", input[0], input[1], input[2]);
    sprintf(second, "%c %c %c %c", input[3], input[4], input[5], inpput[6]);
    printf("%s\n", input);
    printf("%s\n", first);
    printf("%s\n", second);
}
Cusx
  • 1
  • 4

2 Answers2

2

There is no string in c, you can use strncpy to get first few characters as aswered there: Strings in c, how to get subString

int main (int argc, string argv[])
{
    char* input = argv[1];
    char first[4];
    char second[5];

    strncpy(first, input, 3);
    strncpy(second, input + 3, 4);

    first[3] = second[4] = '\0';
}
Community
  • 1
  • 1
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
1
#include <stdio.h>
#include <string.h>

int main (int argc, char* argv[])
{
    if(argc >= 2)
    {
        const int len = strlen(argv[1]) / 2;
        char str1[len + 2], str2[len + 2];
        snprintf(str1, len + 1, "%s", argv[1]);
        snprintf(str2, len + 2, "%s", argv[1] + len);
        printf("1: %s\n2: %s\n", str1, str2);
    }
    return 0; 
}
someuser
  • 189
  • 1
  • 11