string - 如何在生成的连接字符串中包含空格('')?
问题描述
分配给我的问题是,当给出连接字符串的输出时,第一个和第二个字符串之间必须有一个空格'',而且,禁止使用内置函数“strcat()”。例如 String1=Hello,String2=World,ConcatenatedString=Hello(space)World。我需要帮助。谢谢你。
void strconcat(char s1[15], char s2[15])
{
int i;
printf("ENTER A STRING : ");
gets(s1);
printf("ENTER A STRING : ");
gets(s2);
while (s1[i] != '\0')
{
i++;
}
for (int j = 0; s2[j] != '\0'; j++, i++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
puts(s1);
}
解决方案
如果允许使用内置的 c 函数,size_t strlen(const char *str)
则可以保存第一个 while 循环。
void strconcat(char s1[15], char s2[15])
{
int i = 0; // You have not initialized this in your code.
printf("ENTER A STRING : ");
gets(s1);
printf("ENTER A STRING : ");
gets(s2);
i = strlen(s1); // If the use of in-built strlen is not allowed then just use the below two lines
s1[i] = ' '; // Here i is at the position of '\0' character in s1 array.
i++;
for (int j = 0; s2[j] != '\0'; j++, i++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
puts(s1);
}
推荐阅读
- java - 如何获取firebase cloud firestore中集合的总数?
- amazon-web-services - 如何在 aws lambda 函数中获取 api 网关密钥名称
- vbscript - 有没有办法从对 ADO 命令查询的响应中获取列列表?
- javascript - 带有 Ajax 调用的 wordpress PHP
- django - django-extra-views 中的 form_valid 方法。实际形式_valid
- excel - 在工作表之间自动拉取相应的数据?
- java - 使用 sdk 版本 28 时如何修复 GeoFire 以在实时 firebase 数据库中保存位置
- performance - 有哪些方法可以提高 AWS Elasticsearch 服务的性能?
- java - 如何避免向下转换(我需要向下转换列表