Solved

How do I trim a char array?

Posted on 2003-11-23
11
553 Views
Last Modified: 2010-04-15
Hi all ,

How do I trim a char array? .

I have declared a char array for the the size=10 .
A dynamic string , which length will be veried from 0 to 10  is te assigned
with this char array.

If the dynamic string length less than 10 , then i need to trim the extra spaces from
 the char array . How do i do that . I think , its very basic qustion in c , but iam new to
C world .

Help highly apprciated .

Regards,
Yasin.
0
Comment
Question by:expertyasin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
11 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9808755
>If the dynamic string length less than 10 , then i need to trim the extra spaces from
>the char array . How do i do that .

for a statically defined array, this is not possible ...
e.g. if you have

char a [10];

then there is no way to to make it 11 or 9 ever (unless you change value of 10 and recompile)

what you can do however, is allocate memory dynamically .. this will allow you to change sizes at run time

Moreover, you can allocate only as much as you require ...

char * a;

a = (char *) malloc ( strlen(input) + 1 );
strcpy ( a, input );

if you wish tochange size to say 20, then

a = (char *) realloc ( a, 20 );
0
 

Author Comment

by:expertyasin
ID: 9809295

Actually my input string is coming from a function which is out of my scope . In this case , is it possible to identify and remove the extra memory spaces ?.




0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9809304
if that function declared it as char a[10] , then no there is no way to resize it...

if it used malloc to get the string, then yes it can be trimmed using realloc
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:expertyasin
ID: 9809370

 The input string memory is allocated using malloc only .

 Once i get the input string i need to replace its part of the content
 using a  "dynamic" string.

For Ex :

input string : Sky Train
size = 14

i need to replace the   with a number say 160 .
(actually this replacement string size may vary).

then the output string will be :  "Sky160Train"
size=11


--------------------the code as follows -------------------

char *output;
char ch;

output =  (char *)malloc(strlen(input));

for(int k=0;k<strlen(input);k++)
{
     ch=input[k];


     // here the replacement
     if (ch == '&')
     {
                for (glyphptr = glyph;(ch = input[++k]) != EOF && (glyphptr - glyph) < 15;  
                glyphptr ++)
       {
               if (ch == ';' || isspace(ch))   break;
               else *glyphptr = ch;
       }                                                
        if (glyphptr == glyph)
        {
                       ch = '&';
        }
        else
        {                               
           *glyphptr = '\0';      
            ch = iso8859(glyph); //dynamic string generated here
        }
        }

        output[p++] = ch;                          
  }

output= (char *) realloc ( output, p );


-------------------------------------------------------------------------------------

actually it doesnt works . The memory trailing spaces are not removed .

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809386
>The memory trailing spaces are not removed
1. How did you detemine this ?
2. did you initialize p ?
0
 

Author Comment

by:expertyasin
ID: 9809444

Yes . I initialized the p value as 0 .
I printed the Input and its size .          They are Sky&nbsp;Train and 14
I printed p value after for loop , before realloc function its 11
After realloc i printed the Output and its size .
They are Sky160Train--- and the size is still  14

Very strange . But i think , i am doing something wrongly .But i cant able to
identify .


0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809507
did you place the end of string marker at appropriate place in output ?
strlen() gives you length of string which you are getting as 14 (so your EOS is wrongly placed)
0
 

Expert Comment

by:marCello
ID: 9819272
Sorry to interupt sunnycoder,
I find that this may help:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char* FunctionOutOfScope(char  unedited[]){
    // do whatever
    return (char*) unedited;
}

char* ReplaceChars(char edited[], char toRemove[], char toAdd[]){
    char* final;
    char* aux;
    int A, B, C, D, i, j, k, position;

    A = strlen(toRemove);
    B = strlen(toAdd);
    C = strlen(edited);
    D = strlen(edited) - strlen(toRemove) + strlen(toAdd);

    final = (char*) malloc (D+1);
    aux = (char*) malloc (D+1);

    aux = strstr(edited,toRemove);
    position =(int) (aux - (char*)edited);
    k=0;

    if(aux){
        for(i=0 ; i<position ; i++)
            final[i] = edited[i];
        final[i+1] = '\0';
        strcat(final,toAdd);
        for(j=position+A; j<=C; j++)
            aux[k++] = edited[j];
        aux[j+1] = '\0';
        strcat(final,aux);

        return (char*)final;
    }

    free(final);
    free(aux);
    return NULL;
}

int main(){
    char * s;

    s=(char*) malloc (strlen(FunctionOutOfScope("Sky&nbsp;Train")+1));
    strcpy(s,FunctionOutOfScope("Sky&nbsp;Train"));
    printf("Initial String :%s\n",s);
    printf("Edited String :%s\n",ReplaceChars(s,"&nbsp;","160"));

    free(s);
    return 0;
}
0
 

Expert Comment

by:marCello
ID: 9819354
i am trying to improve still, so that you can realloc s string to receive the function ReplaceChars. That would do what you are looking for.

Regards
Marcello
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question