# string manipulation problem

Page 1 of 1

## 12 Replies - 1936 Views - Last Post: 27 May 2007 - 01:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=28293&amp;s=6f56451d1d4ca6939c2905df5338de31&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 the juice

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 11-May 07

# string manipulation problem

Posted 22 May 2007 - 09:35 AM

i want to manipulate this string so that it prints out,

1st character, 4th character, 8th character and soon.
msg[0] , msg[3] , ms[7]

so far the code has only been working if i write 'i++;' instead of 'i+4'.

```int i = 0;
while(msg[i] !=0){
printf("%c",msg[i]);
i+4;
}

```

if some one knows how to do this it would be a great help cheers.

Is This A Good Question/Topic? 0

## Replies To: string manipulation problem

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: string manipulation problem

Posted 22 May 2007 - 09:44 AM

the juice, on 22 May, 2007 - 09:35 AM, said:

so far the code has only been working if i write 'i++;' instead of 'i+4'.

```int i = 0;
while(msg[i] !=0){
printf("%c",msg[i]);
i+4;
}

```

You need i=i+4;

```int i = 0;
while(msg[i] !=0){
printf("%c",msg[i]);
i=i+4;
}

```

### #3 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

## Re: string manipulation problem

Posted 22 May 2007 - 10:46 AM

or i+=4;

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: string manipulation problem

Posted 22 May 2007 - 10:48 AM

manhaeve5, on 22 May, 2007 - 10:46 AM, said:

or i+=4;

I was thinking that... for about a second. But I allways end up with variable=+value; & then the program doesn't compile.

### #5 the juice

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 11-May 07

## Re: string manipulation problem

Posted 23 May 2007 - 07:08 AM

when the string is being printed out the first few letters come out correct, but then after that a group of random ascii characters come out is there anyway of preventing this. i guess this is because it is going over the EOF marker but i dont know how to stop this.

cheers

This post has been edited by the juice: 23 May 2007 - 07:09 AM

### #6 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: string manipulation problem

Posted 23 May 2007 - 07:19 AM

the juice, on 23 May, 2007 - 07:08 AM, said:

when the string is being printed out the first few letters come out correct, but then after that a group of random ascii characters come out is there anyway of preventing this. i guess this is because it is going over the EOF marker but i dont know how to stop this.

cheers

You're getting those charactors because you declair your array to take up X amount of memory, but then you don't use it. So when you run a loop that prints the entire array, it will print whatever is in that space. If you want to clean that up, write a for loop to null ever charactor in the array after you declair the array, but before you use it.

```#define MAX 256
char buff[256];
int i;
for(i=0;i<=MAX;i++) buff[i]='\0';

```

### #7 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

## Re: string manipulation problem

Posted 23 May 2007 - 08:47 AM

no2pencil, on 22 May, 2007 - 10:48 AM, said:

manhaeve5, on 22 May, 2007 - 10:46 AM, said:

or i+=4;

I was thinking that... for about a second. But I allways end up with variable=+value; & then the program doesn't compile.

if you do that it does the same as i=4;

### #8 the juice

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 11-May 07

## Re: string manipulation problem

Posted 24 May 2007 - 02:57 AM

You're getting those charactors because you declair your array to take up X amount of memory, but then you don't use it. So when you run a loop that prints the entire array, it will print whatever is in that space. If you want to clean that up, write a for loop to null ever charactor in the array after you declair the array, but before you use it.

```#define MAX 256
char buff[256];
int i;
for(i=0;i<=MAX;i++) buff[i]='\0';

```

[/quote]

i tried using this but it just ends up nulling all of the characters i have entered but not the ones which were already null, they just stay as ascii characters.

This post has been edited by the juice: 24 May 2007 - 02:58 AM

### #9 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: string manipulation problem

Posted 24 May 2007 - 05:36 AM

the juice, on 24 May, 2007 - 02:57 AM, said:

```#define MAX 256
char buff[256];
int i;
for(i=0;i<=MAX;i++) buff[i]='\0';

```

i tried using this but it just ends up nulling all of the characters i have entered but not the ones which were already null, they just stay as ascii characters.

How are you assigning the value to the array?

This post has been edited by no2pencil: 24 May 2007 - 05:37 AM

### #10 the juice

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 11-May 07

## Re: string manipulation problem

Posted 24 May 2007 - 07:19 AM

i am using gets(string); as i want to include spaces

### #11 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: string manipulation problem

Posted 27 May 2007 - 02:38 AM

the juice, on 22 May, 2007 - 09:35 AM, said:

i want to manipulate this string so that it prints out,

1st character, 4th character, 8th character and soon.
msg[0] , msg[3] , ms[7]

so far the code has only been working if i write 'i++;' instead of 'i+4'.

```int i = 0;
while(msg[i] !=0){
printf("%c",msg[i]);
i+4;
}

```

if some one knows how to do this it would be a great help cheers.

The reason that you get random crap after the inital letters is because of your exit condition for the loop. While it is true that every C-String is zero terminated, it is not true that this happens on a multible of 4 all the time. Maybe if you were to use something like:
```int i = 0;
while(msg[i] !=0 && i<=8){
printf("%c",msg[i]);
i += 4;
}
```
Or a char-for-char scan:
```while(msg[i] !=0){
if (i % 4 == 0) { printf("%c",msg[i]); }
i++;
}
```

BTW this brings up a point... 0+4 != 3. None of these examples really functions correctly.

### #12 the juice

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 11-May 07

## Re: string manipulation problem

Posted 27 May 2007 - 06:07 AM

nice one mate that works grand, cheers

### #13 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: string manipulation problem

Posted 27 May 2007 - 01:32 PM

I guess I should point out that arrays in C/C++ are just pointers. The following is perfectly valid:
```int Array[10];
Array[10000]=45;
```
Of course there may be various errors caused by such irresponsible coding. You have no idea what section of memory Array[10000] refers to, you could very well be overwriting code, overwrting other data. C/C++ makes no checks to make sure that you are keeping within the bounds of your array.

the avriable above Array is nothing more than a pointer to an allocated section of memory. For example:
```int Array[10];
int *PtrToArray = Array;
int i;
for (i=0; i< 10; ++i)
{
PtrToArray[i]=i;
Array[i]++;
}
for (i=0; i< 10; ++i) { Printf("%d ", Array[i]); }
```

will output 1 2 3 4 5 6 7 8 9 10 because PtrToArray and Array are both pointer pointing to the same block of memory.

The array index operator [ ] is equivalent to a offset dereferance. Array[x] is equivalent to *(Array + x). So the compiler makes no check to ensure that you are within the bounds of your array. In dynamic arrays the compiler has no real way of knowing the size of the array anyway.