-1

I'm trying to trim a string (remove white spaces from the start & end of the string) using pointers for that.

char* killspace(char *a)
{
    char *enda = NULL;
    int i = 0, spaceS = 0, spaceE = 0, bigend = 0 , out = 0, taille = strlen(a); 
    do
    {
        if (a[i] == ' ')
        {
            spaceS++;   
        }
        else
        {
            out = 1;
        }
        i++;
    } while (out == 0);
    out = 0;
    do
    {
        if (a[taille] == ' ')
        {
            spaceE++;
        }
        else
        {
            out = 1;
        }
        taille--;
    } while (out == 0);
    bigend = (spaceE + spaceS);
    // new size
    enda = (char *)calloc((strlen(a)-bigend), sizeof(char));
    i = 0;
    for (int j = spaceS; j < (strlen(a)-spaceE); j++)
    {
        enda[i] = a[j];
        i++;
    }
    return(enda);
    free(enda);
    
}

bigend is the number of whitespaces at the beginning and at the end of the string.

but the returned result had some random char like "ýýýý««««««««îþîþîþ"

Amir Saadallah
  • 668
  • 1
  • 8
  • 19
  • 3
    You forgot to null-terminate your character pointer by adding a '\0' at the end of it. – John Odom Nov 17 '14 at 22:26
  • 3
    What is the `free()` after the unconditional `return` supposed to achieve? – EOF Nov 17 '14 at 22:28
  • i put free to release the memory , and there is no condition for return – Amir Saadallah Nov 17 '14 at 22:32
  • The `free` is never executed. Your compiler will warn you of that. You did enable warnings and then read them right? – David Heffernan Nov 17 '14 at 22:35
  • possible duplicate of [How do I trim leading/trailing whitespace in a standard way?](http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way) – Bill Lynch Nov 17 '14 at 22:35
  • Note: this code does not eliminate "white-space", but only "space" characters. Example: to skip leading "white-space", use `isspace()`. `while (isspace((unsigned char) *a)) a++;` – chux - Reinstate Monica Nov 17 '14 at 22:48
  • @AnonyPlng : the `free`, when put after the `return`, will never be executed. When your function reaches `return`, it ends it execution at that exact point. Unfortunately, the question is formed in such a way that you can't count on getting good answers : please familiarize yourself with an article [explaining how to ask a good question](http://stackoverflow.com/help/how-to-ask). – Daniel Kamil Kozar Nov 17 '14 at 22:52
  • @DanielKamilKozar sorry my english is bad :( – Amir Saadallah Nov 17 '14 at 22:57
  • i forget to finish it with '\0' i pute in the end enda[i] = '\0'; and it's work – Amir Saadallah Nov 18 '14 at 20:43

2 Answers2

3

Changing the beginning address to a string, requires either (1) sending the address to the pointer holding the string as an argument so it can be changed or (2) returning a pointer to the new beginning of the trimmed string from the function. The latter is probably your best bet. Here is an example:

#include <stdio.h>
#include <ctype.h>

char *trimstr (char *s)
{
    while (isspace(*s))     /* while space, increment pointer   */
        s++;

    char *p = s;            /* pointer to string s              */
    while (*p) p++;         /* advance pointer to end of s      */
    p--;                    /* decrement pointer off null-term  */

    while (isspace(*p))     /* while space, set new end of str  */
    {
        *p = 0;
        p--;
    }

    return s;               /* return pointer to trimmed string */
}

int main () {

    char mystring[] = "  some string with leading/trailing WS  ";
    char *p = mystring;

    printf ("\n  Original String: '%s'\n\n", mystring);

    p = trimstr (mystring);

    printf ("  Trimmed String : '%s'\n\n", p);

    return 0;
}

output:

$ ./bin/trimstr

  Original String: '  some string with leading/trailing WS  '

  Trimmed String : 'some string with leading/trailing WS'

Approaching the problem this way generally results in shorter code that trying to do the "index-shuffle" to move all characters downward in the string to cover the leading whitespace. However, there is nothing wrong with the "index-shuffle", you just have to be particular with the offset and remember to also offset the null-terminator.

If you are interested in saving lines of code, a more compact, albeit slightly less-readable version of the trimstr function can be written as follows:

char *trimstr (char *s)
{
    while (isspace(*s)) s++;        /* while space, increment pointer   */
    char *p = s;                    /* pointer to string s              */
    while (*p) p++;                 /* advance pointer to end of s      */
    while (isspace(*(--p))) *p = 0; /* while space, set new end of str  */
    return s;                       /* return pointer to trimmed string */
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    Glad it was useful to you. Take some time and step through the pointer logic used. It is a really good example to help cement use of pointers in C. A great way to do it would be to take the string `' ws '` as input, then on a piece of paper diagram what is happening. Pointers are one of the most bewildering (until learned) but powerful concepts in C. – David C. Rankin Nov 17 '14 at 23:20
0

i foubd the problem in my case i pute at the end of string a limiter enda[i] = '\0'; and it work for me

Amir Saadallah
  • 668
  • 1
  • 8
  • 19