如何选择何时在字符数组中打印特定字符 [英] How do I select when to print specific characters in a character array

查看:237
本文介绍了如何选择何时在字符数组中打印特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的作业,我需要执行以下操作:

For my assignment I need to do the following


  • 请求用户输入他们的姓名并将其保存为字符数组

  • 然后我需要输出用户输入的字符数。

  • 然后我必须向后拼写用户名。

  • 然后告诉用户字符数组的哪个部分包含他们的姓和名之间的空格

  • 我需要做的最后一件事,但不能安静(实际上我完全失去了)输出用户的姓氏首先,然后名字最后(即John Doe = Doe John);

  • Ask the user to input their name and save it to a character array
  • Then I need to output the number of characters input by said user
  • Then I have to spell that user's name backwards
  • followed by telling the user which part of the character array contained the space between their first and last name
  • The last thing that I need to do but can't quiet figure out (actually I'm completely lost) is output the user's last name first then first name last (ie John Doe = Doe John);

#include <stdio.h>
#include <string.h>

int main ()
{
printf("Enter your name\n");

char name [25]={'\0'};
char * pch;

fgets(name,sizeof(name),stdin); 

printf("You Entered: %s     \n", name);

printf("There are %u characters in your name including the space. \n", strlen(name));

char end;
int i;
end = strlen(name) -1;
printf("Your name backwards is");
for (i = end; i >= 0; --i)
{
printf("%c", name [i]);
}

printf("\nLooking for the space in your name \n", name);
pch=strchr(name, ' ');
while (pch!=NULL)
{
    printf("Space found at %d\n", pch-name+1);
    pch=strchr(pch+1, ' ');
}


}


任何知道如何做的人你能解释你做了什么,所以我可以向你学习吗?

Anyone that knows how to do this Can you please explain what you did so I can learn from you?

推荐答案


我需要做的最后一件事, b $ b我完全丢失)输出用户姓氏首先然后首先
名称最后(即John Doe = Doe John);

The last thing that I need to do but can't quiet figure out (actually I'm completely lost) is output the users last name first then first name last (ie John Doe = Doe John);

这是一个常见的任务/面试问题,如何颠倒数组中的字符串。一个优雅的虽然可能不是最有效的算法是:

It is common assignment/interview question of how to reverse an oder of strings in an array. An elegant albeit maybe not the most efficient algorithm would be:

step 1: reverse the array character by character 
step 2: reverse each string in-place

检查答案此处了解更多信息。

Check the answers here for more info.

这篇关于如何选择何时在字符数组中打印特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆