一道OJ上的题

qq_33046541 2016-12-02 09:46:50
原题:http://codeup.cn/problem.php?cid=100000580&pid=1
题目描述
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。

输入
输入一行:待处理的字符串(长度小于100)。

输出
可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。

样例输入
if so, you already have a google account. you can sign in on the right.
样例输出
If So, You Already Have A Google Account. You Can Sign In On The Right.
我的代码如下,个人认为没问题但通不过,求指教:
#include<cstdio>
#include<cstring>
int main()
{
char szA[120];
int nLen;
int j;
while(gets(szA)!=NULL)
{
j=0;
nLen=strlen(szA);
if(szA[0]>=97&&szA[0]<=122)\\第一个字母单独判断
szA[0]-=32;
for(int i=0;i<nLen;i++)
{
if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
j=i;\\遇到指定符号记录
else if(j==i-1)
if(szA[i]>=97&&szA[i]<=122)
szA[i]-=32;\\若某个字母处于指定符号后且为小写,则转换为大写
}
puts(szA);
}
return 0;
}
...全文
161 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33046541 2016-12-03
  • 打赏
  • 举报
回复
引用 7 楼 qq_36409192 的回复:
[quote=引用 6 楼 qq_33046541 的回复:] [quote=引用 5 楼 qq_36409192 的回复:] [quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。 [/quote] 程序有问题,我稍微改了下。
#include <cstdio>
#include <cstring>
#include <cctype> 
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
			{
				if(szA[i+1]>=97&&szA[i+1]<=122)
				szA[i+1]-=32;
			}
		}
		puts(szA);
	}
	return 0;
}
[/quote]我原来的写法为什么错了呀?
ck2333 2016-12-03
  • 打赏
  • 举报
回复
引用 6 楼 qq_33046541 的回复:
[quote=引用 5 楼 qq_36409192 的回复:] [quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。 [/quote] 程序有问题,我稍微改了下。
#include <cstdio>
#include <cstring>
#include <cctype> 
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
			{
				if(szA[i+1]>=97&&szA[i+1]<=122)
				szA[i+1]-=32;
			}
		}
		puts(szA);
	}
	return 0;
}
ck2333 2016-12-03
  • 打赏
  • 举报
回复
引用 10 楼 qq_33046541 的回复:
[quote=引用 9 楼 qq_36409192 的回复:] [quote=引用 8 楼 qq_33046541的回复:][quote=引用 7 楼 qq_36409192 的回复:] [quote=引用 6 楼 qq_33046541 的回复:] [quote=引用 5 楼 qq_36409192 的回复:] [quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。 [/quote] 程序有问题,我稍微改了下。
#include <cstdio>
#include <cstring>
#include <cctype> 
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
			{
				if(szA[i+1]>=97&&szA[i+1]<=122)
				szA[i+1]-=32;
			}
		}
		puts(szA);
	}
	return 0;
}
[/quote]我原来的写法为什么错了呀? [/quote] 你把你写的自己试试就知道问题在哪里了。。[/quote] 我试过,样例可以通过啊[/quote] 我这边显示你把第一个单词的第二个字母也改成大写了,而且程序是否正确不能只用一组数组验证,你多试试。
qq_33046541 2016-12-03
  • 打赏
  • 举报
回复
引用 9 楼 qq_36409192 的回复:
[quote=引用 8 楼 qq_33046541的回复:][quote=引用 7 楼 qq_36409192 的回复:] [quote=引用 6 楼 qq_33046541 的回复:] [quote=引用 5 楼 qq_36409192 的回复:] [quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。 [/quote] 程序有问题,我稍微改了下。
#include <cstdio>
#include <cstring>
#include <cctype> 
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
			{
				if(szA[i+1]>=97&&szA[i+1]<=122)
				szA[i+1]-=32;
			}
		}
		puts(szA);
	}
	return 0;
}
[/quote]我原来的写法为什么错了呀? [/quote] 你把你写的自己试试就知道问题在哪里了。。[/quote] 我试过,样例可以通过啊
ck2333 2016-12-03
  • 打赏
  • 举报
回复
引用 8 楼 qq_33046541的回复:
[quote=引用 7 楼 qq_36409192 的回复:] [quote=引用 6 楼 qq_33046541 的回复:] [quote=引用 5 楼 qq_36409192 的回复:] [quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。 [/quote] 程序有问题,我稍微改了下。
#include <cstdio>
#include <cstring>
#include <cctype> 
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
			{
				if(szA[i+1]>=97&&szA[i+1]<=122)
				szA[i+1]-=32;
			}
		}
		puts(szA);
	}
	return 0;
}
[/quote]我原来的写法为什么错了呀? [/quote] 你把你写的自己试试就知道问题在哪里了。。
qq_33046541 2016-12-02
  • 打赏
  • 举报
回复
引用 5 楼 qq_36409192 的回复:
[quote=引用 4 楼 qq_33046541 的回复:] [quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。[/quote]还是错误。。
ck2333 2016-12-02
  • 打赏
  • 举报
回复
引用 4 楼 qq_33046541 的回复:
[quote=引用 2 楼 qq_36409192 的回复:] 应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误[/quote] 你的符号有问题,不能在中文输入的状态下输入符号,英文和中文这两种状态输出的符号是不同的。我帮你重新打了一遍,你再试试我这个打的。
#include <cstdio>
#include <cstring>
int main()
{
	char szA[120];
	int nLen;
	int j;
	while(gets(szA)!=NULL)
	{
		j=0;
		nLen=strlen(szA);
		if(szA[0]>=97&&szA[0]<=122)
			szA[0]-=32;
		for(int i=0;i<nLen;i++)
		{
			if(szA[i]==' '||szA[i]=='\t'||szA[i]=='\r'||szA[i]=='\n')
				j=i;
			else if(j==i-1)
			if(szA[i]>=97&&szA[i]<=122)
				szA[i]-=32;
			puts(szA);
		}
		
	}
	return 0;
}
PS:如果符号的位置有问题,你自己稍微改下。
qq_33046541 2016-12-02
  • 打赏
  • 举报
回复
引用 2 楼 qq_36409192 的回复:
应该是符号问题,你的符号可能是中文状态下输入的。
我本机运行可以,提交上去显示答案错误
qq_33046541 2016-12-02
  • 打赏
  • 举报
回复
引用 2 楼 qq_36409192 的回复:
应该是符号问题,你的符号可能是中文状态下输入的。
什么意思。。。
ck2333 2016-12-02
  • 打赏
  • 举报
回复
应该是符号问题,你的符号可能是中文状态下输入的。
ck2333 2016-12-02
  • 打赏
  • 举报
回复
while(gets(szA)!=NULL)就意味着你会循环输入szA字符串

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧