带指针的c strcat
我正在尝试使用C语言中的指针和strcat。这是我学习过程的一部分 其思想是用户输入一个包含数字的字符串,输出只返回数字。 因此,如果用户输入带指针的c strcat,c,pointers,strcat,C,Pointers,Strcat,我正在尝试使用C语言中的指针和strcat。这是我学习过程的一部分 其思想是用户输入一个包含数字的字符串,输出只返回数字。 因此,如果用户输入 te12abc输出应为12 这是我第一次尝试: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define SIZE 10 int main() { char palavra[SIZE
te12abc
输出应为12
这是我第一次尝试:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 10
int main()
{
char palavra[SIZE];
char palavra2[SIZE];
char *pont = palavra;
char *pont2 = palavra2;
printf("Insert the string\n");
scanf("%s", palavra);
do{
if (isdigit(*pont)){
strcat(palavra2, *pont);
}
*pont++;
}while (*pont != '\0');
printf("\nThe number is:\n%s\n", palavra2);
return 0;
}
这又一次给我带来了strcat的问题
进行了最后一次尝试,但没有指针,strcat仍然无法工作。代码如下:
int main()
{
char palavra[SIZE];
char palavra2[SIZE];
char temp;
char *pont = palavra;
char * pont2 = &temp;
printf("Insert the string\n");
scanf("%s", palavra);
do{
if (isdigit(*pont)){
temp = *pont;
strcat(palavra2, pont2);
}
*pont++;
}while (*pont != '\0');
printf("\nThe number is:\n%s\n", palavra2);
return 0;
}
int main()
{
int i = 0;
char palavra[SIZE];
char palavra2[SIZE];
char temp;
printf("Insert the string\n");
scanf("%s", palavra);
do{
if (isdigit(palavra[i])){
temp = palavra[i];
strcat(palavra2, palavra[i]);
}
i++;
}while (palavra[i] != '\0');
printf("\nThe number is:\n%s\n", palavra2);
return 0;
}
你能给我指一下正确的方向吗?别担心,我还能做什么
问候,
favolas删除*(取消引用)
strcat需要的是char*
而不是char
但是这个版本附加了其余的部分。
您必须创建以nul结尾的字符串
而且*是无用的
*pont++;
这就行了
pont++;
立刻
int main()
{
char palavra[SIZE];
char palavra2[SIZE];
char c2[2] = "a";
char *pont = palavra;
char *pont2 = palavra2;
printf("Insert the string\n");
scanf("%s", palavra);
do{
if (isdigit(*pont)){
c2[0] = *pont;
strcat(palavra2, c2);
}
pont++;
}while (*pont != '\0');
printf("\nThe number is:\n%s\n", palavra2);
return 0;
但是,这太复杂了
int main()
{
char palavra[SIZE];
char palavra2[SIZE];
printf("Insert the string\n");
scanf("%s", palavra);
char *pont = palavra;
char *pont2 = palavra2;
while (true) {
char c = *pont ++;
if (c == 0) break;
if (isdigit(c)){
*pont2++ = c;
}
};
printf("\nThe number is:\n%s\n", palavra2);
return 0;
你让事情变得更难了。您不需要
strcat
:
/* Untested. */
char *pont = palavra;
char *pont2 = palavra2;
while (*pont) {
if (isdigit(*pont))
*pont2++ = *pont;
pont++;
}
*pont2 = 0;
您的第三次尝试几乎是正确的 替换
strcat(palavra2, palavra[i]);
与
我正在传递palavra+I
而不是palavra[I]
因为前者是一个前进指针,后者是一个字符,strncat
需要指针
这是一个很好的例子来说明如何连接字符串和字符
此外,请确保始终初始化变量
char palavra[SIZE]="";
char palavra2[SIZE]="";
代码的问题:(第1版) 1) 您确实可以使用strcat,但是
*pont
指的是单个字符,而不是以null结尾的字符串
2) 您需要*pont++
但是*pont是一个值,不是指针
将此更改为第1个版本:应该可以
do{
if (isdigit(*pont)){
*pont2=*pont;
pont2++;
}
pont++;
}while (*pont != '\0');
*pont2='\0';
一个小问题,据我所知,OP希望在字符串中附加所有数字,因此例如“a1b2c3d”将执行三个附加<代码>while(*pont){if(isdigit(*pont)){*pont2++=*pont;}++pont;}*pont2=0,我想。@cnicutar感谢这项工作,但是如果我插入
te1te2te3te4te5
程序就会崩溃。我相信这是因为字符串的大小被定义为小于10pont2,只有在复制了字符(即isdigit(*pont))@ThiruValuVar时才应该增加它,这是可行的,但如果我插入te1te2te3te4te5,程序就会崩溃。我相信这是因为字符串的大小被定义为小于10。它不应该停在*pont!='\0'
?是的,没错。您可以使用malloc()动态分配内存或增加大小值。否。在字符串结束之前没有“\0”\0'仅位于字符串“te1te2te3te4te5”的末尾,该字符串将覆盖不属于您的程序的内存的其他部分。有时可能有用。但这显然是错误的。strcat(palavra2,palavra+i,1)
给出了错误错误:函数“strcat”的参数太多了。
我说的是strncat
,而不是strcat
:)我的错:)它工作正常,但问题与其他所有函数一样。如果我插入te1te2te3te4te5
程序崩溃!请参见将SIZE
更改为更大的值,例如20。我知道更改大小是可行的。很抱歉没有早点告诉你。我想把尺寸保持在10
char palavra[SIZE]="";
char palavra2[SIZE]="";
do{
if (isdigit(*pont)){
*pont2=*pont;
pont2++;
}
pont++;
}while (*pont != '\0');
*pont2='\0';