编写字符串首字母的程序
program to write initials of string
我正在尝试制作一个程序,让用户输入他的名字,然后是中间名,然后是姓氏,每个名字后跟 space,就像这个例子:
示例输入:mark brown ashraf
示例输出:m b a
当我调试时我得到 "access violation error"
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
char name[100];
int i=0;
printf("Enter the name :");
gets(name);
while (name[i]!='[=10=]')
{
if (name[i]==' ')
{
i++;
printf("%c",name[i+1]);
}
i++;
}
getch();
}
"now it dont give me the first initial but printed the rest of initials "
while (name[i]!='[=10=]')
{
if (name[i]==' ')
{
i++;
printf("%s",name[i+1]);
}
i++;
}
因为您在循环内递增 i
两次,所以您可能会越界访问数组。这是一个潜在的错误。
gets()
不再是标准,您应该改用 fgets()
来处理缓冲区溢出。
发生访问冲突,因为您在 printf
调用中使用了错误的格式说明符。
printf("%s", name[i+1]);
应该是
printf("%c", name[i+1]);
或者更直接:
putchar(name[i+1]);
发生违规,因为 %s
需要一个指针,然后打印函数取消引用该指针。您传递的 char
值不是有效的内存地址。
请打开编译器警告。他们通常会告诉您您的格式字符串与参数不匹配。
编辑 除了访问冲突,你的程序还有更多问题:
- 您在 space 之后递增
i
两次,这样您实际上打印了每个单词的第二个字母。 (如果您的输入是 "Taylor C Huckleberry",甚至是 space)。
- 除非您的输入以 space 字符开头,否则您不会听清第一个单词。
- 当您的输入有尾随 space 时,您最终可能会打印
'[=18=]'
字符。
- 如果您的输入有后续的 space,您可以打印额外的 space。
还有一些形式化的编程错误:
main
应该 return 和 int
,但您的代码永远不会。
- 您使用已被更安全的
fgets
取代的已过时的 gets
。 (不幸的是,fgets
保留了一个尾随换行符,但这对您的代码应该无关紧要。)
在我看来,更好的方法是跟踪先前读取的字符并仅当前一个字符是 space 而当前字符是字母时才打印初始字符。 header <ctype.h>
提供了方便的函数 isspace
和 isalpha
来检查这一点。先前读取的字符以 space 字符开头,因此您可以捕捉到第一个单词:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char name[100];
char prev = ' '; /* pretend there's a space before the string */
int n = 0; /* number of initials printed */
int i = 0;
printf("Enter the name: ");
fgets(name, sizeof(name), stdin);
while (name[i]!='[=13=]') {
if (isalpha(name[i]) && isspace(prev)) {
if (n++) putchar(' ');
putchar(name[i]);
}
prev = name[i];
i++;
}
putchar('\n');
return 0;
}
我正在尝试制作一个程序,让用户输入他的名字,然后是中间名,然后是姓氏,每个名字后跟 space,就像这个例子:
示例输入:mark brown ashraf
示例输出:m b a
当我调试时我得到 "access violation error"
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
char name[100];
int i=0;
printf("Enter the name :");
gets(name);
while (name[i]!='[=10=]')
{
if (name[i]==' ')
{
i++;
printf("%c",name[i+1]);
}
i++;
}
getch();
}
"now it dont give me the first initial but printed the rest of initials "
while (name[i]!='[=10=]')
{
if (name[i]==' ')
{
i++;
printf("%s",name[i+1]);
}
i++;
}
因为您在循环内递增 i
两次,所以您可能会越界访问数组。这是一个潜在的错误。
gets()
不再是标准,您应该改用 fgets()
来处理缓冲区溢出。
发生访问冲突,因为您在 printf
调用中使用了错误的格式说明符。
printf("%s", name[i+1]);
应该是
printf("%c", name[i+1]);
或者更直接:
putchar(name[i+1]);
发生违规,因为 %s
需要一个指针,然后打印函数取消引用该指针。您传递的 char
值不是有效的内存地址。
请打开编译器警告。他们通常会告诉您您的格式字符串与参数不匹配。
编辑 除了访问冲突,你的程序还有更多问题:
- 您在 space 之后递增
i
两次,这样您实际上打印了每个单词的第二个字母。 (如果您的输入是 "Taylor C Huckleberry",甚至是 space)。 - 除非您的输入以 space 字符开头,否则您不会听清第一个单词。
- 当您的输入有尾随 space 时,您最终可能会打印
'[=18=]'
字符。 - 如果您的输入有后续的 space,您可以打印额外的 space。
还有一些形式化的编程错误:
main
应该 return 和int
,但您的代码永远不会。- 您使用已被更安全的
fgets
取代的已过时的gets
。 (不幸的是,fgets
保留了一个尾随换行符,但这对您的代码应该无关紧要。)
在我看来,更好的方法是跟踪先前读取的字符并仅当前一个字符是 space 而当前字符是字母时才打印初始字符。 header <ctype.h>
提供了方便的函数 isspace
和 isalpha
来检查这一点。先前读取的字符以 space 字符开头,因此您可以捕捉到第一个单词:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char name[100];
char prev = ' '; /* pretend there's a space before the string */
int n = 0; /* number of initials printed */
int i = 0;
printf("Enter the name: ");
fgets(name, sizeof(name), stdin);
while (name[i]!='[=13=]') {
if (isalpha(name[i]) && isspace(prev)) {
if (n++) putchar(' ');
putchar(name[i]);
}
prev = name[i];
i++;
}
putchar('\n');
return 0;
}