我试着把字符串分开,把名字和姓氏分开打印出来。我目前的工作允许我这么做,但我必须手工检查每个名字。我希望有人知道如何做到这一点,但我必须使用相当常见的函数,如strpcy等。有可能使用for循环来做到这一点吗?
非常感谢,感谢您的帮助!
// ConsoleApplication24.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <string.h>
void main() {
char names[10][51] = {
"Pat Townsend", "Michele Kelley", "Yolanda Franklin",
"Willard Benson", "Ashley Simmons", "Shawn Lawson",
"John Phelps", "Mildred Wheeler", "Lucy Mendoza",
"Kelvin Barker" };
char firstnames[10][51];
char surnames[10][51];
int i = 0;
strncpy_s(firstnames[i], names[i], 4);
printf("%s", firstnames);
/* for (i < 1; names[i][i] != ' '; i++) {
firstnames[i][i] = names[i][1];
printf("Name = %s", firstnames);
*/ (please ignore this, it's just me trying}
}
最佳答案
你可以用这样的方法:
strncpy_s(firstnames[i], names[i], strcspn(names[i]," "));
strcspn
包含在string.h
中。