[英]Name in string c++
My task this time is to write program that will create string where you input last and first name and then program prints it in form : Persons last name is _ 这次我的任务是编写程序,该程序将在您输入姓氏和名字的地方创建字符串,然后程序以以下形式打印该字符串:人员的姓氏是_ and first name is
第一个名字是 .
。 Here is my code:
这是我的代码:
#include <stdio.h>
#include <string.h>
void imeosobe(char *s,char *prezime,char *ime)
{
int i,a=0,k=0;
for(i=0;i<strlen(s);i++)
{
if(a==0) *(prezime+i)=*(s+i);
else
{
*(ime+k)=*(s+i);
k++;
}
if(s[i]=="" ) a=i;
}
*(prezime+i-k)='\0';
*(ime+k)='\0';
}
main()
{
char s1[50],prezime[50],ime[50];
printf("Unesi prezime i ime:");
gets(s1);
imeosobe(s1,prezime,ime);
printf("\nPrezime je %s,a ime %s ",prezime,ime);
return 0;
}
I think that main problem is in if(s[i]==" " ) a=i; 我认为主要问题在于if(s [i] ==“”)a = i; since last and first name must be separated with empty space (1 block=enough for 1 letter (one click of space)) but I'm not sure how to correct it
由于姓氏和名字之间必须用空格隔开(1个块=足以容纳1个字母(单击一次空格)),但是我不确定如何更正它
You need to change 你需要改变
if(s[i]=="")
to 至
if(s[i] == ' ') // s[i] is a char, not a string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.