C Program that split an input string into two output strings
Posted by Samath
Last Updated: May 04, 2016
  2913

Write code (mysplit.c) to split an input string (variable “name”) into two output strings (variables “first” and “last”). Assume that the user provides input containing only the characters a through z and A through Z. Assume there are exactly two capital letters in the input, one at the beginning of the first name, and one at
the beginning of the last name. For example, given the input “JoeSmith”, your code should split it into “Joe” and “Smith”. Your code should use the following lines:

char name[50],first[25],last[25];
printf("What is your name? ");
scanf("%s",name);

Solution:

#include<stdio.h>
#include<string.h>
int main()
{
	char name[50], first[25], last[25];
	int i,j,k;
	printf("What is your name? ");
	scanf("%s", name);
	for (i = 1; i < strlen(name); i++){
		if (name[i] >= 65 && name[i] <= 90){
			j = i - 1;
			break;
		}
	}
	for (k = 0; k <= j; k++)
		first[k] = name[k];
	first[k] = '\0';
	for (j = 0;name[i]!='\0'; j++){
		last[j] = name[i];
		i++;
	}
	last[j] = '\0';
	printf("%s\n", first);
	printf("%s\n", last);
	return 0;
}