Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hi

Posted on 2006-05-25
4
Medium Priority
?
307 Views
Last Modified: 2010-04-15
hi all,
 I have a problem in this small project. For example, if input is "hi hello day" , output should be" day hello hi".
 I got stuck on concatinate after successful strtok(). Please help.

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

char * extend_reverse (char *string);
 
void main (void)
{
      char input[20]="\0";
    char *ptr1= input;
      char *ptr2;

      printf("Enter you input : " );
      fgets(input,20,stdin);

      printf("Your orginal input: ");
      printf("%s\n",input);

      printf("Your extend reverse input :" );
      ptr2 = extend_reverse(ptr1);
//      printf("%s\n",ptr2);

}

//strlen() Returns the number of characters in
//string before the terminating null-character.
char *extend_reverse (char *string)
{
      // add 1 more room for NULL due to return of strlen()
    static char *output2= (char* )malloc(strlen(string)*sizeof(char)+1);
      memset(output2,'\0',sizeof(output2));
      int i = strlen(string);
      char *temp = string;

      char *array[500];

      char *pch;
      pch = strtok (temp," ");
      int t = 0;
      array[t]= (char*) malloc(strlen(pch)*sizeof(char));
      strcpy(array[t],pch);
      t++;

      while (pch != NULL)
      {
            pch = strtok (NULL, " ,.");
            if( pch!= 0)
            {
                  array[t]= (char*)malloc(strlen(pch)*sizeof(char)+1);
                  memset(array[t],'\0',sizeof(array[t]));
                  strcpy(array[t],pch);
                  t++;
            }
            else
                  break;
      }
      t--;

      int x=0;
      char *ptr= output2;


      //stuck from here
      for(x = 0; x<=t; x++)
      {
            if ( x==0)
            {
                  strcpy(ptr,array[t]);
            }
            else
                  strcat(ptr,array[t]);

          ptr= ptr + sizeof(array[t]);
            *ptr =' ';
            ptr++;
            *ptr='\0';
            t--;
      }

      //to there

      
    output2[strlen(output2)] ='\0';


      temp = output2 ;
    return temp;
}
0
Comment
Question by:valleytech
  • 2
4 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 16764445
one problem is with your memset()'s.

char * var;  
sizeof( var ) always equals 2, or 4, or 8, depending on the compiler.

that's the size of the pointer, not what is pointed to.

maybe you mean strlen( var ) ?

same thing where you increment ptr by sizeof(array[t]);


>   output2[strlen(output2)] ='\0';  

That's useless.  You're putting a zero byte right where strlen() found a zero byte.

0
 

Author Comment

by:valleytech
ID: 16764668
oh.let me try to fix it. Thanks.
0
 

Author Comment

by:valleytech
ID: 16765350
#include <stdio.h>
#include <stdlib.h>

struct node{
      int data;
      node* next;
};

void add (node *head, int a);
void printList(node *head);

void main(void)
{
      int a;
      int yes;
      node *head =NULL;

      printf("do you want to add node? 1 for Yes:  ");
      scanf("%d",&yes);
      while ( yes == 1)
      {
            printf("Enter a new number : ");
            scanf("%d",&a);
            add (head, a);

            printList(head);
            
            printf("do you want to add node? 1 for Yes:  ");
          scanf("%d",&yes);
      }

}

void printList(node *head)
{
      node *tmp= head;

      while(tmp->next != NULL)
      {
            printf(" %d ",tmp->data);
      }
}
void add (node *head, int a)
{
      node *tmp= head;
      node* ptr= new node;
      ptr->data= a;
      ptr->next = NULL;
      
      if(head == NULL) //no node at all
      {
            head = ptr;  
      }
      else
      {
            while(tmp->next != 0)
            {
                  tmp= tmp->next;
            }
            tmp->next= ptr;
      }
}


 For above problem, I forget how to make the value of head still valide after this function return back to main() ? Thanks for advise. I forgot how to do it.
0
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
ID: 16766435
You do not have modified head. You just us tmp as  a cursor which you use to run through the whole list. So head still points to the head of the list. Just a small remark. You adding at the end is the most expensive way to add things to a list. The better idea would be adding at the front of the list and may be reverse that list later.

Regards
Friedrich
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question