诱人的老师中文字幕在线观看
遊客:  註冊 | 登錄 | 幫助





標題: [求助] 教教我。。。。真样把 hello 弄去 h e l l o ????
  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2006-10-28 04:00 AM  資料  個人空間  短訊  加為好友 
教教我。。。。真样把 hello 弄去 h e l l o ????

X-Seek
此 X-Seek 主題價值 50 XS。 (天藍出手)
X-Seek 出題者 onlylonely 希望能在 2006-11-4 04:00 AM 前尋找到解決方案。
已於 2006-10-31 03:16 AM 由 onlylonely 選出最合適的解決方案及關閉 X-Seek 主題。

#include <stdio.h>

int main()
{
        char string1[20],string2[]="string literal";

        int i;

        printf("enter a string:");

        scanf("%s",string1);

        printf("string1 is:%s\nstring2 is:%s\n"
                        "string1 with spaces btw characters is:\n",
                        string1,string2);

        for( i=0; string1 !='\0'; i++)
                printf("%c",string1);

        printf("\n");

        return 0;
}

这是我老师给的。。。。都不能得。。。。。教教我。。。。plz.........



[ 觀看解決方案 ]


最後編輯: mickeyGoUp : 2006-10-29 11:14 PM
頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2006-10-28 04:40 AM  資料  個人空間  短訊  加為好友 
您的老師給您的答案無錯呢!  您所指的 "不能" 是甚麼?  compile 時有 error?  還是在 run 時見不到 "h e l l o"?  在 run 時﹐他會叫您填入一個 string﹐如您填 "xxxxx"﹐他應該是會顯示:

string1 is:xxxxx
string2 is:string literal
string1 with spaces btw characters is:
x x x x x

xxxxx 當然就是您所輸入的 string。

請再提供多點資料。



最後編輯: mickeyGoUp : 2006-10-28 04:46 AM
頂部



  Steven仔     Rank: 3
青出藍
性別 保密
UID 8071

精華 0
帖子 68
積分 170   詳情

閱讀權限 40
註冊 2006-8-20
來自
狀態 離線

 
 
 
 
發表於 2006-10-28 08:12 PM  資料  個人空間  短訊  加為好友 
你那個program我run過.. 沒問題..
你要有hello出來.. 你要自己打一個string
你run完program之後.. 他要你"enter a string:"
之後打你想要的東西.. 就會出來..如hello

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2006-10-29 12:30 AM  資料  個人空間  短訊  加為好友 
如果我打hello我的output 也好像是 hello....没有分开。。。。。。。。我没放 string 为什么你们可以compile出来的???
for( i=0; string1 !='\0'; i++)。。。。。。。。。。。。。。是 for(i=0; string1!=''\0'; i++)。。。。。。。。。。。。。。

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2006-10-29 12:37 AM  資料  個人空間  短訊  加為好友 
enter a string:tony
string1 is:tony
string2 is:string literal
string1 with spaces btw characters is:
tony
Press any key to continue

頂部

lhy     Rank: 3
青出藍
性別 男
UID 14043

精華 0
帖子 121
積分 330   詳情

閱讀權限 40
註冊 2006-10-16
來自 Netherlands
狀態 離線

 
 
 
 
發表於 2006-10-29 08:43 AM  資料  個人空間  短訊  加為好友 
First of all

#include <stdio.h>

int main()
{
        char string1[20],string2[]="string literal";

        int i;

        printf("enter a string:");

        scanf("%s",string1);

        printf("string1 is:%s\nstring2 is:%s\n"
                        "string1 with spaces btw characters is:\n",
                        string1,string2);

        for( i=0; string1 !='\0'; i++) <-- 1
                printf("%c",string1); <-- 2+3

        printf("\n");

        return 0;
}


  • This will end up in endless loop? didnt specifyed which position in the String1 char array to look for the 0 terminator.
  • When I compile this thing and I run it, my result will not be my input string! It print out P. Doesnt supprise me because again didnt give up pos too look up in the array.
  • Why should it print spaces between chars? There is totally nothing that says to add a space



Try this out

#include <stdio.h>

int main()
{
        char string1[20],string2[]="string literal";

        int i;

        printf("enter a string:");

        scanf("%s",string1);

        printf("string1 is:%s\nstring2 is:%s\n"
                        "string1 with spaces btw characters is:\n",
                        string1,string2);

        for( i=0; string1[i] !='\0'; i++)
                printf("%c ",string1[i]);

        printf("\n");

        return 0;
}

enter a string:tony
string1 is:tony
string2 is:string literal
string1 with spaces btw characters is:
t o n y




X-Seek 解決方案 1/1   


最後編輯: lhy : 2006-10-29 08:59 AM
頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2006-10-29 12:39 PM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 lhy 於 2006-10-29 08:43 AM 發表
First of all

#include <stdio.h>

int main()
{
        char string1,string2[]="string literal";

        int i;

        printf("enter a string:");

        s ...

cool man............i get it..........i think is nothing problem with the programs my lec give........but i think is my visual c++ got problem....i check back the program u write n lec write.....excatly same .................so i think is my problem.............anyway.......thx u......

頂部

lhy     Rank: 3
青出藍
性別 男
UID 14043

精華 0
帖子 121
積分 330   詳情

閱讀權限 40
註冊 2006-10-16
來自 Netherlands
狀態 離線

 
 
 
 
發表於 2006-10-29 07:05 PM  資料  個人空間  短訊  加為好友 
it is not the same!!

Your teacher gave you this:

        for( i=0; string1 !='\0'; i++)
                printf("%c",string1);

And what I have changed in to is this

        for( i=0; string1[i] !='\0'; i++)
                printf("%c ",string1[i]);

And do you really understand the code? Do you know c++?



最後編輯: lhy : 2006-10-29 07:22 PM
頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2006-10-29 11:13 PM  資料  個人空間  短訊  加為好友 
lhy 係喎!!!  幣喇!  我真係可能年紀大喇!!    當我見到佢個 loop 時﹐在我腦中我自動幫佢轉左做 array﹐又自動幫佢加左個 space 入去.. (當然我係無 run 過佢啦)。

走漏眼添!  都係您細心!

頂部

lhy     Rank: 3
青出藍
性別 男
UID 14043

精華 0
帖子 121
積分 330   詳情

閱讀權限 40
註冊 2006-10-16
來自 Netherlands
狀態 離線

 
 
 
 
發表於 2006-10-30 07:05 AM  資料  個人空間  短訊  加為好友 
我Surely係細心 ;). 當我見到佢個 loop 時﹐我腦中自動幫我 finish the loop. But when I run it. I saw the problem in the loop



最後編輯: lhy : 2006-10-30 07:07 AM
頂部

快速美言
           


當前時區 GMT+8, 現在時間是 2024-5-6 07:07 AM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.015113 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver