Re: [Dev-C++] who can help solve these two questions?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-05-17 10:59:06
|
Three hints to send to your friend.
1: A string pointer (char*) can't be assigned to a character array
(char[]). Instead string copy must be used. strcpy(a,tokenptr);
2: The last parameter to strtok() only takes a string of DIFFERENT
characters that may delimit the keywords, such as " \t\r\n". To split a
string separated by multiple characters, the split must be manually done,
for example with the strstr() function to search for substrings ("000") in
the original string.
3: Never offer to solve someones student work. Always limit yourself to
giving hints. What use is school assignments if someone else does the
work? In a real world, there are no team-mates around that will offer
always step in and do all the work...
/Per W
On Fri, 16 May 2003, iad929 wrote:
> A friend of mine who is a student asked me to help her to slove the problems
> in the two homeworks she is working on. She said that she gave up trying to
> solve them. She sent to me for help but I can't do that because I am moving
> to my new house. Here are the two homeworks and her wrong solutions. I wish
> someone can fix the problems.
>
>
> a) Write a program that will read one line of text that has 2 names
> separated with $. Each name is made up of a first name and last name (see
> the example). Your program is to tokenize the line and compare the two last
> names. If the names are the same you should print "brothers", otherwise
> print "not brothers" the format is :last$first
> for example:mohammed alhadi $ahmed alhadi$
>
> #include<iostream.h>
> #include<string.h>
> int main()
> {
> char s[40];
> char a[40];
> char *tokenptr;
> int i,result;
>
> cout<<"enter the name:";
> cin.getline(s,40);
> cout<<"the toknize are :\n";
> tokenptr=strtok(s," $");
> while(tokenptr!=NULL)
> {
> cout<<tokenptr<<"\n";
> tokenptr=strtok(NULL," $");
> a[i]=tokenptr;
> cout<<a[i];
> i++;
> tokenptr=strtok(NULL," $");
> }
> result=strcmp(a,s);
> if(result==0)
> cout<<"are brothers";
> else
> cout<<"not brothers\n";
> return 0;
> }
>
> b) write a program that will read one line of text that contains four
> students' names and grades separated with three zeros(see the example). your
> program is to parse (tokenize) the line and put the names in an array and
> grades in another array, then you should output the array showing the
> content of the array in a report of the following from
>
> name grade
> ali c
>
> Input format:
>
> Name grade000 name grade000 name grade0000 name grade000
>
> Example: Mohamed a000ahmed b000ali c000 hala d000
>
>
> Your program must read the line at once
> #include<iostream.h>
> #include<string.h>
> int main()
> {
>
> char D[5][5];
>
>
> cout<<"enter the names and the grade";
> cin.getline(D,5);
>
> cout<<"the toknize are :\n";
> tokenptr=strtok(D,"000");
> while(tokenptr!=NULL)
> {
> cout<<tokenptr<<"\n";
> tokenptr=strtok(NULL,"000");
> }
> return 0;
> }
>
>
> Mo
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: If flattening out C++ or Java
> code to make your application fit in a relational database is painful,
> don't do it! Check out ObjectStore. Now part of Progress Software.
> http://www.objectstore.net/sourceforge
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|