C++ Builder

La Isla Bonita



Replace Spaces in a c string
Replace Spaces in a c string


Description

This example shows how to replace the spaces in a c string by a given character.

char*  TForm1::Replace_SpacesIn_C_str( char* s, char c )
{
    char sNoSpaces[500];
    int i = 0;
    int j = 0;

    while (s[j] != '\0')
    {
       if (s[j] == ' ')
           sNoSpaces[i] = c;
       else
           sNoSpaces[i] = s[j];

       i++;
       j++;
    }

    sNoSpaces[i] = '\0';       //PUT THE TERMINATOR
    return sNoSpaces;
}

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.