Re: shorten string length by 1



John Smith wrote:
Jason wrote:

John Smith wrote:


I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp=  strlen(aGroup);
for (i=0; i< temp;i++)
	 printf(" %d %d %c\n", i+1,temp,aGroup[i]);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.


Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?

-Jason
-Jason



Thanks for the reply.

As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.

BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.


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

int main(void)
{
   char aGroup[2048];
   int i;
   strcpy(aGroup, "Hello Sailor");
   i = strlen(aGroup);
   while (i--) {
      puts(aGroup);
      aGroup[i] = '\0';
   }
   return 0;
}

Why is this thread so long?
--
Joe Wright                            mailto:joewwright@xxxxxxxxxxx
"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---
.



Relevant Pages

  • Re: c# interview question
    ... particular version of the compiler happens to generate. ... internally will call Concat(string, string). ... shows the second loop runs 5-10pc quicker than the first loop. ... int s1 = System.Environment.TickCount; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)