您的位置:首页 > 其它

关于句子翻转的几个问题

2015-08-09 12:26 465 查看

第一种翻转

Sample Input

1

3

I am happy today

To be or not to be

I want to win the practice contest

Sample Output

I ma yppah yadot

oT eb ro ton ot eb

I tnaw ot niw eht ecitcarp tsetnoc

int main()
{
int m,j;
char b[1000];
scanf("%d",&m);
getchar();
while (m--){
scanf("%d",&j);
getchar();
while (j){
scanf("%s",b);
int len = strlen(b);
for (int i = len-1;i >= 0;i--){
printf("%c",b[i]);
}
if (getchar() == '\n')
{
j--;
printf("\n");
}
else
printf(" ");
}
if (m)
printf("\n");
}
return 0;
}


第二种翻转

Input:
english is very interesting
Output:
interesting very is english
void swap_str(char str[],int start,int end)
{
int low = start;
int high = end;
while (low < high){
// swap(str[low], str[high]);
char temp = str[high];
str[high] = str[low];
str[low] = temp;
low++;
high--;
}
}

void rev(char str[]){
int len = strlen(str);
swap_str(str,0,len-1);
int i2 = 0;
int i3 = 0;
for(int i1 = 0; i1 < len;i1++){
if ((str[i1] == ' ')||(i1 == len-1)){
i3 = i1;
swap_str(str,i2,i3-1);
i2 = i3+1;
}
}

}

int main()
{
char s[1000],c;
int i = 0;
//scanf("%s",s);
//int len = strlen(s);
/*while (getchar() != '\n'){
}
printf("%s\n",s);*/
/*scanf("%s",s);
printf("%s\n",s);*/
while ((c=getchar()) != '\n')
s[i++] = c;
s[i] = '\0';
rev(s);
printf("%s",s);<pre name="code" class="cpp">    return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: