题解 | #字符串变形#

字符串变形

https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param s string字符串 
 * @param n int整型 
 * @return string字符串
 */
char* trans(char* s, int n ) {
  char tmp;
int i = 0; int j = 0;
for (i = 0; i<n-1-j; i++)
{
	tmp = s[n - 1 - i];
	s[n-1-i] = s[i];
	s[i] = tmp;
	j++;
}
char* str1 = s; char* str2 = s; char* p=s;
while (*p!='\0')
{
	while (*str2 != ' '&&*str2!='\0')
	{
		str2++;
	}
	p = str2;
	str2--;
	while (str1 < str2)
	{
		tmp = *str2;
		*str2 = *str1;
		*str1 = tmp;
		str2--; str1++;
	}
	str1 = p+1;
	str2 = p+1;
}
str1 = s;
while (*s != '\0')
{
	if (*s >= 65 && *s <= 90&&*s!=' ')
	{
		*s += 32;
	}
	else if(*s>=97&&*s<=122&&*s!=' ')
	{
		*s -= 32;
	}
	s++;
}
   return str1;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务