0

So I'm writing a program that takes a persons name and splits it into their first and last name so for example if you enter JonSnow it should print: First: Jon Last: Snow

This is the code, please ignore the comments, I was testing a bunch of different ways to do it.

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

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

    printf("What is your name? ");
    scanf("%s",name);

    strcpy(first," ");
    strcpy(last," ");
    for(i=0;i<strlen(name);i++){
        strcat(first,name[i]);              //for(j=i+1;strlen(name);j++){
        if(name[i+1]>=65 && name[i+1]<=90){
            strcat(last,name[i]);
            strcat(last,name[i+1]); 
        }
        //}             
    }

    printf("First name: %s \n",first);
    printf("Last name: %s \n",last);  
}

When I run it in the terminal, I get this:

What is your name? JonSnow

Segmentation fault (core dumped)

What is the problem, please help...

4
  • 2
    strcat takes two char* parameters, but you pass in name[i], which is a char. Commented Mar 30, 2017 at 23:43
  • if(name[i+1]>=65 && name[i+1]<=90){ is ok, but isupper is a lot more readable/directly understandable. Commented Mar 30, 2017 at 23:47
  • Just a general comment - assuming there are only 2 capital letters (first one is beginning of first name and second one is beginning of last name) a better algorithm will be: Iterate over your string to find the index of second capital letter. Then using strncpy, copy the first part until this index (excluding) to first, and from this index to the end to last. Commented Mar 31, 2017 at 0:27
  • You should use a width specifier when using %s in scanf() to prevent buffer overflow: scanf("%49s",name);. Commented Mar 31, 2017 at 0:37

1 Answer 1

1

I think your code is close, you're just messing up how to use strcat trying to add a single character to the end of a string, which it doesn't do. Perhaps you can do something like this:

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

int main() {

char name[50],first[25] = {0},last[25] = {0};
int i;

    printf("What is your name? ");
    scanf("%s",name);

    for(i=0;i<strlen(name);i++) {
        if(name[i+1]>=65 && name[i+1]<=90) {
            strncpy(first,name,i+1);
            strcpy(last,&name[i+1]); 
        }
    }
    printf("First name: %s \n",first);
    printf("Last name: %s \n",last);
}

strncpy copies the number of characters specified by i+1 into first.

Sign up to request clarification or add additional context in comments.

2 Comments

Without a width specifier in the call to scanf(), there is a risk of buffer overflow. Also, if the first or last name is longer than 24 characters, this code writes out of bounds. i should be size_t since strlen() returns a size_t value.
All that is true. Also, if there are no capital letters in the input, it will crash too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.