char *ch = "abcde";
char c[4];
how to copy 'ch' to 'c'?

Answers were Sorted based on User's Feedback



char *ch = "abcde"; char c[4]; how to copy 'ch' to 'c'?..

Answer / parth ujenia

main()
{
char *ch="abcd";
char c[4];

for(int i=0;i<4;i++)
{
c[i]=*ch; //assign value to char c[i].
*ch++; //switch to next address of ch!
}

for(i=0; i<4 ;i++)
{
printf("%c - ",c[i]); //output will: a - b - c - d -
}

getch();

}

Is This Answer Correct ?    18 Yes 7 No

char *ch = "abcde"; char c[4]; how to copy 'ch' to 'c'?..

Answer / gopi

main()
{
char *ch="abcd";
char c[4];

for(int i=0;i<4;i++)
{
c[i]=*ch;
ch++;
}


printf("%s",c);

getch();

}

Is This Answer Correct ?    12 Yes 2 No

char *ch = "abcde"; char c[4]; how to copy 'ch' to 'c'?..

Answer / wade stone

#include <stdio.h>
#include <string.h>

using namespace std;

int main( )
{
char *ch = "abcde";
char c[4];

memcpy( c, ch, sizeof( c ) );

return 0;
}

Is This Answer Correct ?    2 Yes 1 No

char *ch = "abcde"; char c[4]; how to copy 'ch' to 'c'?..

Answer / supriya pandey

i think we used the string libruary function strcpy() to
copy it...

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C++ General Interview Questions

What is the difference between the indirection operator and the address of oper-ator?

0 Answers  


What does the following do: for(;;) ; a) Illegal b) Loops forever c) Ignored by compiler...not illegal

0 Answers  


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

0 Answers   CTS,


What are special characters c++?

0 Answers  


What are class and object in C++?

1 Answers  






write asingle linked list which read from two list & the do the following 1 sort the prime & nonprime num (prime should be less tn nonprime) 2 each node has a prime num followd by nonprime 3 add a new node into its sutable plce 4 erase the most three duplicated non prime num 5 find the least duplicated prime num

0 Answers   Care,


How to allocate memory dynamically for a reference?

0 Answers  


Can I learn c++ without knowing c?

0 Answers  


What is the arrow operator in c++?

0 Answers  


Will a catch statement catch a derived exception if it is looking for the base class?

0 Answers  


In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest.

5 Answers   GE,


int main() { int i ,a[i]; i = 0; a[i] = 10; cout<< a[i] << endl; return 0; } What will be output of this program?

11 Answers   HCL, Mphasis,


Categories