[Dev-C++] who can help solve these two questions?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: iad929 <ia...@jo...> - 2003-05-17 00:30:53
|
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
|