Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT - URGENT

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
mgayan (Visitor) Jul 13, 2001
Write a function (ReOrderWord(const char *string )) using a subset of C (see "Restrictions" bellow") to rewrite string "TODAY IS A BEST BOSTON DAY" as "DAY BOSTON BEST A IS TODAY".

Restrictions:
You do NOT have access to string libraries and CANNOT use functions like Stringtok(), strcmp(), etc. Only basic C instructions are allowed (we want to see at how you handle pointers...)

You CAN use any function giving the length (strlen()) or size of a string and functions to copy all or part of a string. You CAN create any additional array, string, list, etc, you think you need to solve the problem.

The function must work with any string containing any number of letters..
----------
Note 1: the input is a nul terminated String. Do NOT use anything else (like a list of the words already tokenized!).
----------
 
Hi mgayan,

Here's what you need...

[ignore]#include<stdio.h>
#include<stdlib.h>

unsigned strlen(const char *);

char *ReOrderWord(const char *line)
{
char *word, *output;
int c, iline, iword=0, len;
void StoreReverse(char *src, char *dest);

len = strlen(line);
word = (char *) malloc(len*sizeof(char) + 1);
output = (char *) malloc(len*sizeof(char) + 1);
*output = '\0';
iline = len - 1;
while(iline >= 0) {
c = (word[iword] = line[iline]);
if(c == ' ') {
word[iword] = '\0';
StoreReverse(word, output);
if(c == ' ') StoreReverse(&quot; &quot;, output);
iword = -1;
}else if(iline == 0) {
word[iword+1] = '\0';
StoreReverse(word, output);
}
iline--; iword++;
}
free(word);
return output;
}

void StoreReverse(char *src, char *dest)
{
int i;
char *d;
d = dest;
i = strlen(src) - 1;
while(*d != '\0')
d++;
while(i >= 0) {
*d = (*(src + i));
i--;
d++;
}
*d = '\0';
return;
}

unsigned strlen(const char *string)
{
unsigned len = 0;
while(*(string + len) != '\0')
len++;
return len;
}[/ignore]

The above ReOrderWord function returns a pointer to the string which has the input 'line' reordered in the way required. You can assign a string like:
char *reordered;
reordered = ReorderWord(&quot;TODAY IS A BEST BOSTON DAY&quot;);
or you could call it by somethng like:
char string = &quot;TODAY IS A BEST BOSTON DAY&quot;
printf(&quot;%s&quot;, ReOrderWord(string));
or just
printf(&quot;%s&quot;, &quot;TODAY ...&quot;);
or so on...

Hope this helps.
Bye. Ankan.

Please do correct me if I am wrong. s-)
 
hello mgayan,

ankan's programme is excellent ,

another alternative ,see if it helps

#include<stdio.h>
#include<string.h>
#include<malloc.h>

char* funcReorder(char *ptr)
{
char *p=ptr;
while(1){
if(*p==' ' || *p=='\0')
break;
p++;
}
*p='\0';
return ptr;
}

main()
{
const char* p=&quot;hello this is for your disposal&quot;;
char *ptr=(char *)malloc(sizeof(p)+1);
char *temp=ptr;


//arr of character pointer
char *arr[20];
int i=0;
strcpy(ptr,p);
while(*ptr)
{
arr=funcReorder(ptr);
ptr=ptr+strlen(arr)+1;
i++;
}
for(int j=0;j<i;j++)
printf(&quot;%s &quot;,arr[i-j-1]);

printf(&quot;\n&quot;);
delete [] temp;


}
compile it run it change the input source to
standard input stream ,make small changes as necessary
see if it helps

bye
yogesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top