2014-09-21 76 views
-1

如果s是一個字符串(說"thisisthe"),如何將第一個字節存儲到另一個字符串s2將字符串的第一個字節存儲到另一個字符串中:LZW壓縮算法

#include <stdio.h> 

int main() { 
    char s[]="thisisthe"; 
    char s1=s[0]; // Store the first byte of s as a char. 
    char s2[]=s[1]; // store the second byte of s as a string. 
    printf("%s\n",s2); 
    return 0; 
} 
+0

你能避免肘粗體字?謝謝。 – 2014-09-21 16:12:02

回答

-1

s2是一個字符,而不是一個字符串。 變化

printf("%s\n", s2); 

printf("%c\n", s2); 

如果你想s2是一個字符串,你應該首先聲明它:

char s2[2]; 
s2[0] = s[1]; 
s2[1] = '\0'; //the terminating character 
+0

對不起,我想製作 'char s2 [] = s [1];' – 2014-09-21 16:22:27

+0

我需要存儲s2作爲一個字符串持有一個字符 – 2014-09-21 16:27:02

+1

「你應該首先聲明它:」具體來說,它應該是「定義它」 – starrify 2014-09-22 04:42:53

2

在C語言中,字符串,通過終止字符數組一個NUL。

char s2[2]; 
s2[0] = s[1]; 
s2[1] = '\0'; 

但是,也許你只是想

char c2 = s[1]; 
printf("%c\n", c2); 
+0

s2 [0] = s2 [1];'typo? – starrify 2014-09-21 16:12:54

+0

@starrify,固定。 – ikegami 2014-09-21 16:13:54

+0

謝謝你的工作:D:D – 2014-09-21 16:41:30

相關問題