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 real purpose of class – to export data?

0 Answers  


What are c++ stream classes?

0 Answers  


What are function poinetrs? where are they used?

1 Answers   CTS,


Can a function take variable length arguments, if yes, how?

0 Answers  


Does c++ have foreach?

0 Answers  






Describe private, protected and public?

0 Answers  


How would you use qsort() function to sort an array of structures?

0 Answers  


When there is a global variable and local variable with the same name, how will you access the global variable?

0 Answers  


What is the role of C++ shorthand's?

0 Answers   TCS,


Write a program to add three numbers in C++ utilizing classes.

0 Answers   TCS,


Can we declare destructor as static? Explain?

3 Answers  


What is the Standard Template Library?

1 Answers  


Categories