How to create a char array dynamically according to the length of user input string?
-
The user should not have to enter the length of the string. e.g if user enters "abcd" the program should create a char array of 5 bytes to store it (1 byte for NULL). similarly if user enters "123456789" the program should create a char array of 10 bytes.
-
Answer:
char *RS() { char x[100], *p; scanf("%s", x); //else you may make x as argument p=(char *) malloc(strlen(x)+1); strcpy(p,x); return p; } replace the following in case of C++ p=new char[strlen(x)+1]; Do make sure you include string.h Now we get the address of the string for which memory is allocated dynamically and occupies number of elements +1 bytes
Ali at Yahoo! Answers Visit the source
Related Q & A:
- How To Create A Profile In Facebook?Best solution by Yahoo! Answers
- How to create a dropdown user control?Best solution by Stack Overflow
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to create a underscore template dynamically?Best solution by Stack Overflow
- How to create a Database in FoxPro and how to retrieve and sort it out?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.