c strcat与指针

时间:2012-04-02 18:18:23

标签: 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];
    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不起作用。

第二次尝试,即程序找到一个数字,将char存储在一个变量中,然后尝试将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;
}

再一次,它给了我strcat的问题。

最后一次尝试,但没有指针,仍然strcat不起作用。这是代码:

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

4 个答案:

答案 0 :(得分:3)

你使它变得比以往更难。您不需要strcat

/* Untested. */   
char *pont = palavra;
char *pont2 = palavra2;

while (*pont) {
    if (isdigit(*pont))
        *pont2++ = *pont;

    pont++;
}
*pont2 = 0;

答案 1 :(得分:3)

第三次尝试几乎是正确的。

替换

strcat(palavra2, palavra[i]);

strncat(palavra2, palavra+i,1);

我正在传递palavra+i而不是palavra[i] cos,前者是一个进步指针而后者是一个字符而strncat需要指针

这是一个很好的例子来说明如何连接字符串和字符

另外,请确保始终初始化变量

char palavra[SIZE]="";
char palavra2[SIZE]="";

答案 2 :(得分:3)

与您的代码相关的问题:(第一版)

1)你做strcat*pont引用单个字符,它不是以空字符结尾的字符串。

2)你做*pont++;但是* pont是一个值,而不是一个指针。

对第一版进行此更改:应该没问题。

 do{
        if (isdigit(*pont)){
            *pont2=*pont;
             pont2++;
        }
        pont++; 
    }while (*pont != '\0');

*pont2='\0';

答案 3 :(得分:1)

删除*(取消引用),

        strcat(palavra2, pont);

strcat期望char*不是char 但这个版本附加了整个休息。 你必须创建一个以空字符结尾的字符串。

而*是没用的

    *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;
相关问题