string

Z

zohaibbaloch

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help
 
L

lndresnick

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help

I don't really understand your question. By "manipulate without using
arrays", do you mean using pointers instead of array indexing? If so,
here is an example of how that works:

char foo[] = "dog";
char first_letter = foo[0]; /* using array indexing */
char second_letter = *(foo+1); /* using pointer */

Is the second what you mean? You could change foo as well, e.g.
to make it "log" you'd do:

*foo = 'l';

If you want to do the manipulation you want (adding a letter), you need
an array big enough. Look into malloc, though for the simple example
you give you could do this:

char foo[5] = "dog"; /* 5 leaves room to add 1 letter */

I suggest reading
http://www.eskimo.com/~scs/C-faq/s6.html
For more about arrays and pointers...

-David
 
O

osmium

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help

If I understand correctly, the assignment makes as much sense as "Build a
wooden house without using a hammer".
 
L

Lawrence Kirby

If I understand correctly, the assignment makes as much sense as "Build a
wooden house without using a hammer".

Or without using wood. It is a fundamental property of C's string that
they reside in arrays.

Lawrence
 
K

Keith Thompson

osmium said:
If I understand correctly, the assignment makes as much sense as "Build a
wooden house without using a hammer".

Less; you can use a nail gun.

I suppose you could implement string-like data structures using linked
lists, but I'm sure that's not what the OP is asking about.
 
M

Mark

/*** putting on flame suit ***/
You obviously don't understand correctly... re-read his question.
Would this satisy the requirements if all he has to do is print the string?

void
without_a_hammer(char *s)
{
if(s && *s) {
char *p = s +1;
while(*p)
putchar(*p++);
printf("%ca\n", *s);
}
return;
}
Or without using wood. It is a fundamental property of C's string that
they reside in arrays.


/*** zipping up flame suit ***/
Who cares what they reside in, he asked how to manipulate the string!
As long as the caller ensured that the 'array' the string 'resides' in can
accommodate an extra byte... what's wrong with this?

void
without_using_wood(char *s)
{
if(s && *s) {
char *p = s +1;
int ch = *s;
while(*p)
*s++ = *p++;
sprintf(s, "%ca", ch);
}
return;
}

/*** starting the fire ***/
OP: here's a driver to test the first example... it will print out the
strings passed on the command line!

void
main(int oh, char *boy[])
{
while(*++boy)
without_a_hammer(*boy);
return; /* NOTHING!!!! HAHAHAHA */
}

/*** giggling with anticipation as I wait for it to burn :) ***/
Regards,
Mark
 
J

Joe Wright

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help

It is not clear what help you need. I would concentrate first on English
sentences, capitalization of the personal pronoun, words which begin
sentences. Oh well.

As mentioned earlier, a string is defined as an array. I'll guess that
you don't want a construct like ..

char array[10] = "dog";

... but something like ..

char *pointer = malloc(10 * sizeof *pointer);
strcpy(pointer, "dog");

... would be OK. Right?

But be sure, array is an array and pointer points to one.

puts(pointer); /* dog\n */
strcat(pointer, "da");
puts(pointer); /* dogda\n */
puts(pointer+1); /* ogda\n */

I'm fairly sure this doesn't solve your problem. That is because I don't
understand what you problem is.
 
K

Keith Thompson

Mark said:
Lawrence Kirby said:
how to manipulate string without using arrays.manipulation example [...]
If I understand correctly, the assignment makes as much sense as "Build a
wooden house without using a hammer".
[...]
Or without using wood. It is a fundamental property of C's string that
they reside in arrays.

/*** zipping up flame suit ***/
Who cares what they reside in, he asked how to manipulate the string!
As long as the caller ensured that the 'array' the string 'resides' in can
accommodate an extra byte... what's wrong with this?
[snip]

A string is an array. You can't manipulate strings without using
arrays. You can certainly manipulate strings without using array
*notation*. That's probably what the OP meant, but it's not what he
said.
 
J

Jason

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help

It sounds like you are looking for some form of 3rd party string
library, right?

Try GNU: http://www.gnu.org/

-Jason
 
J

Jason

how to manipulate string without using arrays.manipulation example

i have to print dog or any string
i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.
please help

Are you simply looking for a 3rd party library, written in C, that will
give you something similar to the string clases provided in languages
like C++ or Java?

If this is what you are looking for, then look through the GNU tools
(www.gnu.org). If this is not what you are looking for, then please
rephrase the question, preferably with some code as an example.

-Jason
 
G

Gregory Pietsch

i have to print dog or any string

fputs("dog", stdout);

i want to cut d from dog and paste it a end and add a at the last
dog becoms ogda.

char *s = "dog";
printf("%s%ca", s+1, *s);

Gregory Pietsch
 
W

Walter Roberson

A string is an array. You can't manipulate strings without using
arrays.

As the requirement had to do with *printing*, you can.

Read the first character. Store it into a char -- a single char
is not an array. Read further characters. If the character is \n
then print the saved character and print an 'a' and print \n and
exit the loop; otherwise print the character.

And if you want to get fancier, then gratitously use ungetc() along
the way.
 
L

Lawrence Kirby

As the requirement had to do with *printing*, you can.

Read the first character.

....

To do that you will have to use an array, because that is there the
character is stored.

Lawrence
 
D

Default User

Walter said:
As the requirement had to do with *printing*, you can.

Read the first character. Store it into a char -- a single char
is not an array. Read further characters. If the character is \n
then print the saved character and print an 'a' and print \n and
exit the loop; otherwise print the character.


Then you aren't talking about a string. The term "string" has a defined
meaning in C. From the C89 draft standard:

4.1.1 Definitions of terms

A string is a contiguous sequence of characters terminated by and
including the first null character. It is represented by a pointer to
its initial (lowest addressed) character and its length is the number
of characters preceding the null character.


Basically, it's a dumb assignment. It teaches the student nothing about
strings or dealing with strings. If the goal was to have the student
write a state machine for processing input from a file a character at a
time (which would be useful in my opinion) then the assignment should
have been written that way.



Brian
 
K

Keith Thompson

Default User said:
Basically, it's a dumb assignment. It teaches the student nothing about
strings or dealing with strings. If the goal was to have the student
write a state machine for processing input from a file a character at a
time (which would be useful in my opinion) then the assignment should
have been written that way.

We don't really know what the assignment was. I suspect the intent
was to manipulate strings without using the array indexing operator
(i.e., using only explicit pointer operations). That would make
sense, and would teach the students about the relationship between
arrays and pointers. The instructor's problem statement may or may
not have been badly worded.

In any case, the OP didn't say anything about processing input from a
file:

] how to manipulate string without using arrays.manipulation example
]
] i have to print dog or any string
] i want to cut d from dog and paste it a end and add a at the last
] dog becoms ogda.
] please help
 
D

Default User

Keith said:
We don't really know what the assignment was.

Ok, basically I was replying to Roberson's reinterpretation of the
problem.
I suspect the intent
was to manipulate strings without using the array indexing operator
(i.e., using only explicit pointer operations). That would make
sense, and would teach the students about the relationship between
arrays and pointers.

That's possible.
The instructor's problem statement may or may
not have been badly worded.

Granted. I have seen a number of assignments that look pretty bad
because they are filtered through the student's brain.
In any case, the OP didn't say anything about processing input from a
file:

No, Roberson did. I was objecting to him calling that string
manipulation.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top