Memove not working!!

I am not getting string copied.
i wanted to copy only the first word.
but some erroneous output
[code]
#include<stdio.h>
#include<string.h>
void main()
{
int n,i;
char s[100],s2[100];
printf("Enter a String: \n");
gets(s);
while(s[0]==32)
strcpy(s,s+1);
n=strlen(s);
while(s[n-1]==32)
{
strcpy(s+n-1,s+n);
n--;
}
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]==32)
break;
}
memmove(s2,s,i+1);
puts(s);
puts(s2);
}
Hi,

i think it just prints out the rest of your string, which is unintialized.
Seems, in case of ony one word, your program works as you expect.

Set the (i+1)'th entry of your string to be the termination sign and it should work as you want it to.

greets,
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<string.h>
void main()
{
	int n, i;
	char s[100], s2[100];
	printf("Enter a String: \n");
	gets(s);
	while (s[0] == 32)
		strcpy(s, s + 1);
	n = strlen(s);
	while (s[n - 1] == 32)
	{
		strcpy(s + n - 1, s + n);
		n--;
	}
	n = strlen(s);

	memmove(s2, s, n + 1);
	puts(s);
	puts(s2);

	getchar();
}
Topic archived. No new replies allowed.