2

I'm trying to use pointers and strcat from C. This is part of my learning process.

The idea is the user inputs a string that contains digits and the output should return only the digits. So, if the user inputs te12abc the output should be 12.

This is my first try:

#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;
}

I believe that the pointer is working as expected but can't understand why strcat is not working.

Made a second try that is the program finds a number, stores that char in one variable and only then try to use strcat with that variable. Here is the code:

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;
}

Once again it gives me problems at strcat.

Made one last attempt but without pointer and still strcat does not work. Here is the code:

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;
}

Can you point me to the right direction? Don't now what more can I do..

Regards,

favolas

Favolas
  • 6,963
  • 29
  • 75
  • 127

4 Answers4

3

You're making it harder than it has to be. You don't need strcat:

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

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

    pont++;
}
*pont2 = 0;
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • A small issue, as I understand, the OP would like to append all digits in the string, so e.g. "a1b2c3d" would do three appends. `while(*pont){ if (isdigit(*pont)) { *pont2++ = *pont; } ++pont; } *pont2 = 0;`, I think. – Daniel Fischer Apr 02 '12 at 18:29
  • @cnicutar Thanks This works but if i insert `te1te2te3te4te5` the program crashes. I believe it's because the size of the string is defined to be less than 10 – Favolas Apr 03 '12 at 10:34
3

Your third try is almost correct.

Replace

strcat(palavra2, palavra[i]);

with

strncat(palavra2, palavra+i,1);

I am passing palavra+i and not palavra[i] cos the former is a progressing pointer and the latter is a character and strncat needs pointers

This is a nice example to illustrate how to concatenate a string and a character

Also, make sure you always initialize your variables

char palavra[SIZE]="";
char palavra2[SIZE]="";
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
3

Porblems with your code: (1st version)

1) You do strcat but *pont refers to single char, which is not a null-terminated string.

2) You do *pont++; But *pont is a value, not a pointer.

Do this change to the 1st version: It should be ok.

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

*pont2='\0';
P.P
  • 117,907
  • 20
  • 175
  • 238
  • pont2 should be incremented only if a char was copied (i.e. isdigit(*pont)) – eyalm Apr 02 '12 at 18:36
  • @Thiruvalluvar Thanks This works but if i insert te1te2te3te4te5 the program crashes. I believe it's because the size of the string is defined to be less than 10. Shouldn't it stops at `*pont != '\0'`? – Favolas Apr 03 '12 at 10:38
  • yes, that's right. You can use malloc() to allocate memory dynamically or increase the SIZE value. – P.P Apr 03 '12 at 10:41
  • No. There's no '\0' until the end of the string. '\0' is there only at the end of string "te1te2te3te4te5" which would have overwritten some other portion of memory that doesnt belong to your program. It may work sometimes. But it's clearly wrong. – P.P Apr 03 '12 at 10:43
1

Remove * (dereference),

        strcat(palavra2, pont);

strcat expects a char* not a char but this version appends the whole rest. you have to create a nul-terminated string.

And the * is useless

    *pont++;

This does the job

    pont++;

now all at once

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;

However, this is too complicated

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;
stefan bachert
  • 9,413
  • 4
  • 33
  • 40