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 c++ virtual inheritance?

0 Answers  


Which sort does c++ use?

0 Answers  


What is the basic structure of c++ program?

0 Answers  


can output 5 students using one dimensional array

1 Answers   Intel,


If we want that any wildcard characters in the command line arguments should be appropriately expanded, are we required to make any special provision? If yes, which?

0 Answers  






How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?

0 Answers  


What is main function in c++ with example?

0 Answers  


What is linked list in c++?

0 Answers  


Why namespace is used in c++?

0 Answers  


What are smart pointer? Whats its use?

1 Answers   CTS,


What are the benefits of oop in c++?

0 Answers  


How can you specify a class in C++?

0 Answers  


Categories