save string
(This is a question for an intership of NVIDIA, Feb. 07, 2007)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char* saveString_Question(char* s);
char* saveString_Answer(char* s);
void main()
{
char* resultFromQ = NULL;
char* resultFromA = NULL;
printf("Input String : InterShip Question from NVIDA.\n\n");
resultFromQ = saveString_Question("InterShip Question from NVIDA.");
printf("Result From Question: %s\n\n", resultFromQ);
resultFromA = saveString_Answer("InterShip Question from NVIDA.");
printf("Result From Answer: %s\n", resultFromA);
}
char* saveString_Question(char* s)
{
char* p = (char*) malloc(strlen(s));
while(*s)
{
*p++ = *s++;
}
*p = '\0';
return p;
}
char* saveString_Question_Answer(char* s)
{
char* p = (char*) malloc(strlen(s) + 1);
char* startPositionOfP = p;
while(*s)
{
*p++ = *s++;
}
*p = '\0';
return startPositionOfP;
}