Hi, i am new to c programming. i still learning the basics. i have been trying writing simple code to get use to the commands. however i am stuck on a problem that i cannot solve. the codes are based on string manipulation.
there are 2 problems i am facing:
1)
Code:
#include<stdio.h>
#include<string.h>
int main(void)
{
char *str1 = "cprogramming";
char *str2 = "this is new";
printf("\n\nstr1 : %s\t\tstr2 : %s\n\n",str1,str2);
printf("\n\nstrncpy(str1,str2,5): %",strncpy(str1,str2,5));
getch();
}
the program is suppose to replace the s first characters of str1 with the first 5 character of str2, the problem is each time 1 run the code, the window stops responding. However when i use
Code:
char str1[80]= "c programming";
char str2[80]= "this is new";
it works..
2)
Code:
#include <stdio.h>
#include <string.h>
void main (void)
{
char *fullname, *firstname, *surname;
printf("Enter Full Name (Surname first): ");
gets(fullname);
firstname = strstr(fullname," ");
strnset(firstname,"",1);
strncpy (surname,fullname,(strlen(fullname)-(strlen(firstname)+1)));
printf("\n\nOutput: %s %s",firstname, surname);
getchar();
}
Here the program was suppose to search for the space between two words entered and stores the word before the space into surname and the word after the space to firstname. but here also the windows stops responding when i run the code.
can someone help me figured where i did something wrong.
thanks.