string manipulation problem

string manipulation problem

Page 1 of 1

12 Replies - 1474 Views - Last Post: 27 May 2007 - 01:32 PM Rate Topic: -----

#1 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: string manipulation problem

Posted 22 May 2007 - 09:44 AM

View Postthe 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;
	 }



Was This Post Helpful? 0
  • +
  • -

#3 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: string manipulation problem

Posted 22 May 2007 - 10:46 AM

or i+=4;
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: string manipulation problem

Posted 22 May 2007 - 10:48 AM

View Postmanhaeve5, 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.
Was This Post Helpful? 0
  • +
  • -

#5 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: string manipulation problem

Posted 23 May 2007 - 07:19 AM

View Postthe 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';


Was This Post Helpful? 0
  • +
  • -

#7 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: string manipulation problem

Posted 23 May 2007 - 08:47 AM

View Postno2pencil, on 22 May, 2007 - 10:48 AM, said:

View Postmanhaeve5, 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;
Was This Post Helpful? 0
  • +
  • -

#8 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,433
  • Joined: 10-May 07

Re: string manipulation problem

Posted 24 May 2007 - 05:36 AM

View Postthe 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

Was This Post Helpful? 0
  • +
  • -

#10 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2245
  • View blog
  • Posts: 9,233
  • Joined: 18-February 07

Re: string manipulation problem

Posted 27 May 2007 - 02:38 AM

View Postthe 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.
Was This Post Helpful? 0
  • +
  • -

#12 the juice  Icon User is offline

  • New D.I.C Head

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

Re: string manipulation problem

Posted 27 May 2007 - 06:07 AM

nice one mate that works grand, cheers
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is online

  • Can grep dead trees!
  • member icon

Reputation: 2245
  • View blog
  • Posts: 9,233
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1