-1

This is my first time I ask , so please can help. My question is how can I Add any character between any string like , mean adding dot after every c (small letter) , but I dont want to use any function , I want to write my own void function with passing only one parameter that should be an array of char , Can help please?

I learn how to check every character in the string with

while(*p!='\0')
{

/// What should I write here to check if there is any dot , then add after it
/// a small c

p++;
}
user3457718
  • 11
  • 1
  • 1
  • 4
  • You checked it if was not null. the process is no different for any other character that you want to compare against. – shawn1874 Mar 25 '14 at 00:56
  • Do you really want to use a C array of char, instead of a C++ std::string? The C array is trickier, and you will also have to pass the length of memory available in the array to your function. Do you have to operate in-place, or can the output string be in a different array? The latter will be easier. – sj0h Mar 25 '14 at 00:59
  • thanks , but I checked it if It's equal to '.' but I cant solve more, another thing I think how the characters inserted inside the ram,I mean array with any number of character,can expend it ?! – user3457718 Mar 25 '14 at 01:02
  • yes,I want to learn that way, but I guess I can do it with pointers – user3457718 Mar 25 '14 at 01:05
  • Welcome to Stack Overflow! Can you show us what have you tried and didn't work? Have you searched the Internet for an answer? Have you searched this site? – Paweł Stawarz Mar 25 '14 at 01:11
  • possible duplicate of [Why does this Seg Fault?](http://stackoverflow.com/questions/3638851/why-does-this-seg-fault) – Paweł Stawarz Mar 25 '14 at 01:14
  • yes,I search in many sites but nothing,I mean this is first question in this way.can anyone help please? – user3457718 Mar 25 '14 at 01:19

1 Answers1

0

If you are going to do it the C way, I'd suggest you try something like this:

void adjust_string(char*output_p, int output_space, const char* input_p)
{
    //while (there is still input left, and room in output buffer) {
    while (*input_p!='\0' && output_space>2) {
        //copy input character to output

        //update the output pointer

        //update the amount of room left in the output buffer

        //if (its a special character) {

             //add the extra character to output

             //update the output pointer

             //update the amount of room left in the output buffer

        }
        //update the input pointer
        input_p++;

    }
    //null-terminate the output string

}

In the function that calls this function, you need to provide an array for the output to be placed into and specify its length, so you can't get a buffer overrun.

Note: in checking for room in the output buffer you need to take into account the possibility of adding the extra character in, and the room for the terminating null character.

sj0h
  • 902
  • 4
  • 4
  • thanks,but like that is more confused,I mean can I pass one parameter that is the base address of the array.and how can I change the occpied bytes in ram(when I add more charaters)? – user3457718 Mar 25 '14 at 01:27
  • Do you really need to do that? The reason I ask is because it makes it much more complicated – sj0h Mar 25 '14 at 01:28
  • You cannot just pass in the base array address, and then proceed to increase the size of that array. At best you can also pass in the length of the memory allocated, and use that to increase the length of the string in that array. However, you can't do this with a const char array, like you get with a string literal. – sj0h Mar 25 '14 at 01:32
  • yes,but I guess It should be easy program.(the logic I used is check the all characters in the string till '\0' and then look for '.' if you found it add small c or whatever char after it),but I cant translate it to complete C program – user3457718 Mar 25 '14 at 01:33
  • I've updated my example to make the statements needed more explicit. Each English comment can be converted into one C statement. Which operations in that do you have problems with? – sj0h Mar 25 '14 at 01:47
  • @user3457718 you are aware that "add small c" would mean inserting and inserting would mean shifting all the memory that comes after it? Which also means you have to check whether to provided buffer is big enough to still hold the content? – PeterT Mar 25 '14 at 03:34