一个改名字的C语言问题!!

来源:百度知道 编辑:UC知道 时间:2024/05/23 17:27:06
程序出来效果是这样的:
Type in a name string with less than 100 characters.
Marsha Lynn Mason<return-key> //这个名字是input
Length of input string name_i is 20
The string name_s after removing extra spaces is:
Marsha Lynn Mason
Length of string name_s is 17
The string name_f with just initials and last name is:
M. L. Mason
Length of string name_f is 11

我写的是:
#include <stdio.h>
#include <string.h>
//Code for function get_name.
void get_name(char*s[100])
{
int c, i=0;
while((c=getchar())!='\n')
{*s[i]=c;
i++;
}
*s='\0';
}
//Code for function remove_spaces.
void remove_spaces (char *s1, char *s2)
{
while (*s1!=' ')
{*s2 =*s1;
s1++;
s2++;}
s2++;
*s2=' ';
while (*s1!=' ')
{*s2 =*s1;
s1++;
s2++;}
*s2++;
*s2=' ';
while (*s1!=' ')

首先,你把题目意思搞错:
Type in a name string with less than 100 characters.
Marsha Lynn Mason<return-key> //这个名字是input
Length of input string name_i is 20

"Marsha Lynn Mason"+回车(2字符)+'\0' 长度等于20(包括'\0')
-----------------------------------------------------
The string name_s after removing extra spaces is:
Marsha Lynn Mason
Length of string name_s is 17

"Marsha Lynn Mason" 长度等于17(去掉回车)
-----------------------------------------------------
The string name_f with just initials and last name is:
M. L. Mason
Length of string name_f is 11

"M. L. Mason" 长度等于11

---------------------------------------------------------------
以下为调试好的程式

#include <stdio.h>
#include <string.h>
//Code for function get_name.
void get_name(char* s)
{
char c ;
while((c=getchar())!='\n