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





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

精華 0
帖子 70
積分 100   詳情

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

 
 
 
 
發表於 2006-10-30 06:55 PM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 lhy 於 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

        ...

No.....my lec give 1 exactly same v ur 1.....but i donno why i cannot paste out [ i ] only.......
actually i not really understand bout the programming....hiah......i think i will fail this subject....
especially is array n pointer .......really hard to understand.............die die die

頂部

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

精華 0
帖子 121
積分 330   詳情

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

 
 
 
 
發表於 2006-10-31 04:28 AM  資料  個人空間  短訊  加為好友 
Well ponter is pretty complicated. So I will leave that part and let your teacher do the job. On the other hand array is pretty simple.

You can see a array as a "cabinet with drawers". in each drawer you can put something in there. Each drawer have a number. The first start at 0.
So in your case means

char string1[20] , string2[]="string literal";

string1 is an char array with length of 20. (a cabinet with 20 drawers).
string2 is an char array with value "string literal".
* a char array means you can only assign 1 char to a index number. In this case size of string2 array will be 15 (remember the 0 terminator).

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

as you can see there we have a for loop. That means the printf command on the second line will repeat over and over again untill the requirement mets on the first line.

On the first line we start with i=0. and than we are looking for string1[ i ] because i is 0 that means it will look up in the index 0 position of string1. if string1[0] is not \0 we need to do the printf command on the second line and increment i with 1. So we start all over again with a new i value untill we find the 0 terminator.

This is what your code do. I hope this will help you.

note * 0 terminator define the end of a string



最後編輯: lhy : 2006-10-31 04:32 AM
頂部

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

精華 0
帖子 70
積分 100   詳情

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

 
 
 
 
發表於 2006-10-31 06:33 PM  資料  個人空間  短訊  加為好友 
Thx you for teaching me alot.....lhy........i got smth not really understand bout u said that ...."That means the printf command on the second line will repeat over and over again untill the requirement mets on the first line."
"string1[0] is not \0 "

Wat u mean  for requirement mets on the first line.....can u explain a bit details???
wat u mean by string1[0] is not \0 .....
\0=null ????

i know i got abit stupid d la..........hopes u will still teach me.....thx u.....



最後編輯: onlylonely : 2006-10-31 06:36 PM
頂部

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

精華 0
帖子 121
積分 330   詳情

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

 
 
 
 
發表於 2006-10-31 08:26 PM  資料  個人空間  短訊  加為好友 
well first of all
How a for loop works

for(i=0; i < 10; i++){
         printf("Hello");
}

In the code above you see for(i=0;i<10;i++).
i=0 --> define where to start
i<10 --> define the condition
i++ --> define what to do when loop ends.

so if you start running the code above the first time you execute the loop i will be 0 (i=0). because of that it meets the loop condition (i<10) so it will execute the printf command. so after the printf command it will increment i by 1 (i++). So we move back to the top and starts again. this time i will be 1 and we do all those steps again untill the value of i is greater than 9. if i do not meets the loop condition it will skip the loop and do what comes after the loop.

So that is the same in your example. while string1[ i ] != '\0' it will loop over and over again untill it finds the '\0'.

\0 is not the same as null. for more info about \0 check this website http://www.cprogramming.com/tutorial/c/lesson9.html

Got it?



最後編輯: lhy : 2006-10-31 08:37 PM
頂部

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

精華 0
帖子 70
積分 100   詳情

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

 
 
 
 
發表於 2006-10-31 09:44 PM  資料  個人空間  短訊  加為好友 
understand a bit liao.............i got read the website u give but still don understand wat is '\0' ...................

'\0' like a space means that if i[10 ] = "hello lhy" .....



最後編輯: onlylonely : 2006-10-31 09:47 PM
頂部

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

精華 0
帖子 121
積分 330   詳情

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

 
 
 
 
發表於 2006-11-1 02:41 AM  資料  個人空間  短訊  加為好友 
Strings in c++ are char arrays. So if you use that you have to define when a string ends. You do this by adding '\0' at the end of the array. When the system see there is a \0 it knows it is the end of a string.

頂部

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

精華 0
帖子 70
積分 100   詳情

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

 
 
 
 
發表於 2006-11-1 03:55 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 lhy 於 2006-11-1 02:41 AM 發表
Strings in c++ are char arrays. So if you use that you have to define when a string ends. You do this by adding '\0' at the end of the array. When the system see there is a \0 it knows it is the en ...

ok.........understand liao....thx u wo...........hehe..................

but i got doubt bout
for example i type hello world'

for(i=0;string[ i ] !='\0';i++)

string[ i ] not equals to '\0' ..........my output why for this will stop in "h e l l o" only....where is the "world" ?????

why after the space cannot print out the "world" ?????

why we don put string[ i ] ='\0' ???? when i put  string[ i ] ='\0' no output come out.........

mean that the loop start at i=0 and stop in string[ i ] ='\0' .......a space in this is represent by '\0' ???

in the program......mean start in i=0 and stop when string[ i ] not equals to \0..........why got a not equals ( ! = )there?????

if we say that start with i=0 and stop when i = space ...........if like that i will understand more.............

do u understand wat i trying to ask????

頂部

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

精華 0
帖子 35511
積分 5235   詳情

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

 
 
 
 
發表於 2006-11-1 06:00 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 onlylonely 於 2006-10-31 03:55 PM 發表
ok.........understand liao....thx u wo...........hehe..................

but i got doubt bout
for example i type hello world'

for(i=0;string[ i ] !='\0';i++)

string[ i ] not equals to '\0' ..........my output why for this will stop in "h e l l o" only....where is the "world" ?????

why after the space cannot print out the "world" ?????

Hmm.. it should take whatever you input, and print out the same string but with space between each character.  Again I have not set up my machine to run C++ codes so I cannot test it for you.  Did it work for lhy?

QUOTE:
原帖由 onlylonely 於 2006-10-31 03:55 PM 發表

why we don put string[ i ] ='\0' ???? when i put  string[ i ] ='\0' no output come out.........

mean that the loop start at i=0 and stop in string[ i ] ='\0' .......a space in this is represent by '\0' ???

in the program......mean start in i=0 and stop when string[ i ] not equals to \0..........why got a not equals ( ! = )there?????

if we say that start with i=0 and stop when i = space ...........if like that i will understand more.............

do u understand wat i trying to ask????

First of all, if you want to say "string[ i ] equals to '\0', don't forget to use == instead of =.  (is that the right syntax for C++? please confirm).

And the way you read the for loop conditions were wrong.  The first part is the initial value; the middle part is to be tested every round (if it is true, the loop continues); and the last part is what will be executed every round from the second round and on.  That's why, we want it to start with i = 0, and continues as long as string[ i ] != '\0' is true, and everytime increment i.

Hope that helps.  



最後編輯: mickeyGoUp : 2006-11-1 06:27 PM
頂部

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

精華 0
帖子 121
積分 330   詳情

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

 
 
 
 
發表於 2006-11-1 05:15 PM  資料  個人空間  短訊  加為好友 
uhm... I have tested your code with "Hello World" seems it doenst work with spaces. I have no idea why.

I have also convert it to work with streams (I though maybe that is it) but no luck either (cin, cout).

the start initial we use 0 because a array index starts at 0.

頂部

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

精華 0
帖子 35511
積分 5235   詳情

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

 
 
 
 
發表於 2006-11-2 11:03 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 lhy 於 2006-11-1 05:15 AM 發表
uhm... I have tested your code with "Hello World" seems it doenst work with spaces. I have no idea why.

I have also convert it to work with streams (I though maybe that is it) but no l ...

You mean it only printed "H e l l o "? Or it did not print the spaces?  You used his codes, the one with "%c" or "%c " ?

頂部

快速美言
           


當前時區 GMT+8, 現在時間是 2024-11-23 03:11 PM

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

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