Thread: String editor for a sentence inputted by a user - any suggestions or ideas?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Sorry if it's only now I've replied to your post...

    To Prelude,

    Sorry if it's only now I've replied to your post - been studying over the source lines you gave me. By the way, there are some things I wish to know about them...

    - What are the advantages of using the function "fgets" over "gets"? (as in the case of this program)

    - Could you please explain how does the function "memmove" act on this line...
    Code:
    memmove ( match, match + len, len );
    If you were to make your own version of the 'memmove' function, what would it look like? (Could you also please include a pseudocode of it?)

    - If I were to make my own version of the function strstr, would it look like the one below?
    Code:
    //Program to find an input substring in an input main string
    
    void main()
    {
     int i=0;
     int j=0;
     int k=0;
     int l=0;
     int k1=0;
     char a[80],b[80];
    
     clrscr();
     printf("Enter main string:- ");
     gets(a);
     printf("Enter sub-string:-");
     gets(b);
    
     l=strlen(b);
     while (a[i]!=EOF) do
           {
            if (a[i]==b[j])
               {
                i++;
       	    j++;
       	    k1=1;
               } 
             else
               {
       	    if (k1==1)
       	       {
        		j=0;
        		k1=0;
       	       }
       	     else
                   i++;
               }
           }
    
      if (k==1)
         {
          printf("The given sub-string is present in the main string.");
         }
       else
         {
          if (k==0)
          printf("The given sub-string is not present in the main string.");
         }
     getch();
    }
    And here's my interpretation of the ideas you gave me
    Code:
    //Edits a string
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<alloc.h>
    #include<mem.h>
    
    
    
    //All the functions to use
    //Searches for the string
    char Find(char main_str, int len)
    {
     char search_str;	//The string to delete
     char *match;		//Contains the string that matches the one to search
     
     printf("String to find:");
     if ( fgets ( search_str, sizeof search_str, stdin ) != NULL ) 
        {
         char *match = strstr ( main_str );
        
         if ( match != NULL )
            printf ( " \"%s\" found at position %d", search_str, match - main_str );
          else
            printf ("The word you want to search does not exist");
        }
     getch();
     return new_str;
    }
    
    
    //Deletes the string
    char Erase(char main_str, int len)
    {
     char del_str;		//The string to delete
     char *del_match;	//Contains the string that matches the one to delete
     char new_str;		//Displays the new string
     int delstr_len;	//length of the string to delete
    
     printf("String to delete:  ");
     if ( fgets ( search_str, sizeof search_str, stdin ) != NULL ) 
        {
         del_match = strstr (main_str);
         if ( del_match != NULL ) 
            {
             delstr_len = strlen ( search_str );
             new_str = memmove ( del_match, del_match + len, len );
             printf ("New source: %s \n", new_str);
            }
          else
            printf ("The word you want to delete does not exist");
        }
     getch();
     return new_str;
    }
    
    
    //Inserts the string - the one you wanted me to try coding on
    char Insert(char main_str,int len)
    {
     int sub_len, total_len;	//variables to use in this function
    
     printf("String to insert: ");
     fgets(sub_str, sizeof sub_str, stdin);	// gets the string from the user
     
     sub_len = strlen (sub_str);		//gets the length of the string to insert
     
     printf("\nPosition of insertion>   ");
     scanf("%d", &pos);			//gets the position as to where to place the string on the main string
     
     total_len = len + sub_len;		//gets the total length of the two strings to use...
     
     char newstring[total_len];		//...as the basis for the length of this array that will hold both of them
     
      // from the starting point of insertion on main_str up to the end of sub_str, insert subtring while incrementing forward
     	for (i=main_str[pos]; i<sub_str[n-1]; i++)	
    						   //does "sub_str[n-1]" access the tail end of sub_str?
    
         newstring = strcat (main_str, sub_str);	//starts inserting the sub_str into main_str using the strcat function
     printf("New source:  %s \n",newstring);	//prints out the string
     
     getch();
     return newstring;
    }
    
    
    
    //the program proper
    void main(void)
    {
     char main_str;		//the first string that the user types in
     int len;		//length of main string
     int choice;		//menu choice number
    
     do
      {
       do
        {		//the menu proper
         clrscr();
         printf("Please type in a sentence:  ");
         fgets(main_str, sizeof main_str, stdin);    //gets main_str
         len = strlen (main_str);       //gets the length of main_str
    
         printf ("\n");
         printf("[1] Find \n");
         printf("[2] Delete \n");
         printf("[3] Insert \n");
         printf("[4] Exit\n \n");
         printf ("\n");
         printf("Enter number of your choice:");
         scanf("%d",&choice);
        } while (choice <1 || choice>4);
    	    switch(choice)
    		  {
    		   case 1:
    			  Find();
    			  break;
    		   case 2:
    			  Erase();
    			  break;
    		   case 3:
    			  Insert();
    			  break;
    		  }
      } while (choice != 4);
    }
    I hope I got your ideas right - well, I'm open to learn new things. Just tell me if I overlooked something.

    Thanks.

    Respectfully yours,
    the_newbug
    Last edited by the_newbug; 03-02-2006 at 04:24 AM. Reason: forgot to add something to my reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM