4

I have a char array which contains some value . I want to copy the value from that array from some random index to some other random index . How can I do that ?

         #include<iostream.h>
         using namespace std;

         int main()
         {
           char ar[100];
           strcpy(ar,"string is strange");
           cout << ar ;
           return 0;
         }

Now ar array contains "string is strange" . Suppose I want to make an another char array cp in which I want to copy the value from random index position of ar say from 7 to 10 . Is there some string function which we can use ? I know we can use strncpy function but it copies from starting index till number of characters mentioned . Is there some other function or an overloaded version of strncpy which will enable me to perform the same?

MSalters
  • 173,980
  • 10
  • 155
  • 350
Invictus
  • 4,028
  • 10
  • 50
  • 80

6 Answers6

2

Do like this

strncpy (dest, ar + 7, 2);

generally

strncpy (destination, source + start_index, number_of_chars);
   The strncpy() function is similar, except that at most n bytes of src are copied.  Warning: If there
   is no null byte among the first n bytes of src, the string placed in dest will  not  be  null-termi‐
   nated.

Therefore you need to null terminate the string manually:

dest[nos_of_chars] = '\0';

UPDATE

You can use something like this:

char *make_substring (char *src, int start, int end)
{
  int nos_of_chars = end - start + 1;
  char *dest;
  if (nos_of_chars < 0)
  {
    return NULL;
  }
  dest = malloc (sizeof (char) * (nos_of_chars + 1));
  dest[nos_of_chars] = '\0';
  strncpy (dest, src + start, nos_of_chars);
  return dest;
}

When you are using C++, do not use the char strings for processing instead use the string class.

phoxis
  • 60,131
  • 14
  • 81
  • 117
2

Your code is in C++, so use the STL - don't create a fixed size character array, use std::string. That has a method substr(pos, n).

so your code would be:

std::string str;
str = "string is not so strange";
cout << str << endl;
std::string small;
small = str.substr(7, 3);
cout << small << endl;

much easier than doing potentially unsafe pointer arithmetic using the C api.

Tom Whittock
  • 4,081
  • 19
  • 24
1

In order to copy n characters starting from position p from string1 to string2, you can use:

strncpy(string2, string1 + p, n);

If you're dealing with C++ strings (std::string), then you can use the substr member function.

std::string string1 = "......";
std::string string2 = string1.substr(p, n);
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1

C++

What are you trying to achieve? 'string is strange' reminds me of spell checking -> permutations

#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::string s = "string is strange";
    std::sort(s.begin(), s.end());

    while (std::next_permutation(s.begin(), s.end()))
        std::cout << s << "\n";

    return 0;
}

To really just exchange random positions: http://ideone.com/IzDAj

#include <random>
#include <string>
#include <iostream>

int main()
{
    using namespace std;
    mt19937 random;

    string s = "string is strange";
    uniform_int_distribution<size_t> gen(0,s.size()-1);

    for (int i=0; i<20; i++)
    {
        swap(s[gen(random)], s[gen(random)]);
        cout << s << "\n";
    }

    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
0

You can take the address of a specific place in the array by using e.g. &ar[i].

For example, if you add the following line before the return

cout << &ar[6] << '\n';

it will print

is strange
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

you can use memcopy function

void * memcpy ( void * destination, const void * source, size_t num );

in your example

memcpy(cp,ar+p,sizeof(char)*n)
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222