ArcticBlueAlex
ArcticBlueAlex

Reputation: 21

splitting strings with C

Basically the program should split the name into F and L names. User puts in their name either combined or with a space (ex. AlexTank or Alex Tank). The program should read in every capital letter and split the string with a space. The issue I have is that my program splits the name (recognizes uppercase letters) but excludes the upper case letters from the new output of the string.

#include <stdio.h>
#include <string.h>

int main()
{
    char name[50], first[25], last[25];
    char *pch;
    char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // ask user for name
    printf("What is your name? ");
    scanf("%s", name);

    printf("Hello \"%s\" here is your First and Last Name:\n", name);
    pch = strtok(name, key);
    while (pch != NULL)
    {
        printf("%s\n", pch);
        pch = strtok(NULL, key );
    }
    return 0;
}

Upvotes: 1

Views: 1123

Answers (2)

Floris
Floris

Reputation: 46365

strtok assumes you do not want the delimiters returned - so it will consume them and return "the rest" (that is, lower case letters only). I would suggest a simpler approach: echo the input string one character at a time; if you see a capital letter but didn't just see a space, add it in. It would look something like this:

#include <stdio.h>

int main()
{
    char name[50];

    // ask user for name
    printf("What is your name? ");
    //scanf("%s", name);
    fgets(name, 49, stdin);

    printf("Hello. Your name is ");
    int ii = 1, foundSpace = 0;
    printf("%c", name[0]);
    while (name[ii] != '\0')
    {
        if (name[ii]==' ') foundSpace = 1;
        if (foundSpace == 0 && isupper(name[ii])) {
          printf(" %c", name[ii]);
        }
        else {
          putchar(name[ii]);
          foundSpace = 0;
        }
        ii++;
    }
    return 0;
}

See if this works for you!

Upvotes: 0

lurker
lurker

Reputation: 58224

There are two issues:

  1. The second parameter to strtok should be ONLY the string of delimiters you want, explicitly. In your case, I think that's just a space (" ").
  2. The %s in the scanf stops reading when it sees the space on the input.

Modified program:

#include <stdio.h>
#include <string.h>

int main()
{
    char name[50], first[25], last[25];
    char *pch;

    // ask user for name
    printf("What is your name? ");
    //scanf("%s", name);
    fgets(name, 50, stdin);

    printf("Hello \"%s\" here is your First and Last Name:\n", name);
    pch = strtok(name, " ");
    while (pch != NULL)
    {
        printf("%s\n", pch);
        pch = strtok(NULL, " ");
    }
    return 0;
}

If you want to allow CamelCase names as well, then strtok won't work on its own since it destroys the delimiter. You could do something simple like pre-process the name and insert spaces, or write a custom tokenizer. Here's the insert space idea method. If you just insert spaces, then the strtok will do what you want:

#include <stdio.h>
#include <string.h>

void insert_spaces(char *in, char *out)
{
    if ( !in || !out )
        return;

    while ( *in )
    {
        if (isupper(*in))
            *out++ = ' ';

        *out++ = *in++;
    }

    *out = '\0';
}

int main()
{
    char in_name[50], first[25], last[25];
    char name[100];
    char *pch;

    // ask user for name
    printf("What is your name? ");
    //scanf("%s", name);
    gets(in_name);

    printf("Hello \"%s\" here is your First and Last Name:\n", in_name);

    insert_spaces(in_name, name);

    pch = strtok(name, " ");

    while (pch != NULL)
    {
        printf("%s\n", pch);
        pch = strtok(NULL, " ");
    }
    return 0;
}

Upvotes: 1

Related Questions