Working with strings!

This is a discussion on Working with strings! within the C Programming forums, part of the General Programming Boards category; I want to write a function that takes two character array parameters, say string1 and string2. Both string1 and string2 ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    Working with strings!

    I want to write a function that takes two character array parameters, say string1
    and string2. Both string1 and string2 contain unknown number of
    characters terminated by !. My function will erase every occurrence of
    string2 in string1.
    Example :
    Your first string : abc!
    Your second string : tabehwabcsje!
    New version of your second string : tabehwsje

    I was able to do that for a predefined number of characters but here in this program the string size is unknown and given by the user. This is what I did, any help would be appreciated!
    ( I wonder how Quzah will get angry for me this time:P!)
    Code:
                                    
    #include<stdio.h>
    
    #define SIZE 50
    
    void func(char [],int ,char [],int );
    
    int main(void)
    {
        char string1[SIZE],string2[SIZE];
        int i,c1=0,c2=0;
        
        printf("Your first string, enter '!' after entering your string to terminate :\n");
        scanf("%s",string1);
         
        for(i=0;string1[i]!='!';i++){                            
                                     c1++;//count element number of string1
                                     }
        printf("%d\n",c1);
        printf("Your second string, add '!' again to the end of your string :\n");
        scanf("%s",string2);
        
        for(i=0;string2[i]!='!';i++){
                                     c2++;//count element number of string2
                                     }
        printf("%d\n",c2);
        func(string1,c1,string2,c2);
        
          
        
        return 0;
    }
    
    void func(char a[],int c1,char b[],int c2){
         int j=0,i=0,stop=0,count=0,k=0;
         
         for(j=0;b[j]!='!';j++){//f1
    
              if(b[j]==a[0]){//i1
    	
                   for(i=0;a[i]!='!';i++){//f2
    
                         if(b[j]==a[i]){//i2
    		j++;
    		count++;	
    		stop=0;
    		printf("olley\n");
    		}//i2
    	
                         if(b[j]!=a[i]){
    		j++;
    		stop=1;
    		count=0;
    		printf("aaaa!\n");
    		}
    							      if((count==c1)&&(stop==0)){//i3
    								for(k=0;k!=count;k++){//f3
    								b[j+count]=b[j];
    		j++;												                                     }//f3									                                                    }//i3
    									}//f2
    		
    								}//i1
    												
         }//f1
        printf("New version of your second string is:\n");
        for(j=0;b[j]!='!';j++){
                                   
                                     printf("%c",b[j]);
                                     
                                     }
    	printf(	"\n");
         }
    Last edited by mass; 05-26-2006 at 04:09 PM. Reason: replacing tabs with spaces

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Edit your post and replace tabs with four spaces. It's too hard to read.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    12
    I couldnt read your function so i wrote my version...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int remove_substring(const char strToFind[], const char strToSearch[], char strModified[], char stop)
    {
        int i;
        int len1 = 0, len2 = 0, len3 = 0;
    
        while(strToFind[len1] != stop && strToFind[len1] != '\0')
             len1++;
    
        while(strToSearch[len2] != stop && strToSearch[len2] != '\0')
            len2++;
        
        if( len1 > len2 || len1 == 0)
        {
           strcpy( strModified, strToSearch );
           return len2;
        }
    
        for( i=0; i<len2; i++ )
        {
             if( strToSearch[i] == strToFind[0] ) 
             if( strncmp( &strToSearch[i], strToFind, len1 ) == 0 )
             {
                 i += len1-1;
                 continue;
             }
             strModified[len3++] = strToSearch[i];
        }
        strModified[len3] = '\0';
        return len3;  
    }
    
    int main ()
    {
        char str1[] = "abc!ttt";
        char str2[] = "qwertabckjhsabcff!f";    
        char str3[50];
        
        remove_substring(str1, str2, str3, '!');
        
        printf("Note! strings 1,2, terminated with %c\n", '!' );
        printf("%s - %s = %s \n", str2, str1, str3 );
        
        getchar();
        
        return 0;
    }
    Last edited by vangmor; 05-27-2006 at 01:50 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    20
    I do not know why I could not post the code sorted and looking good, I just copied it from the source file then I tried to sort. Sorry about that and thanks for helping I have to work on it to understand clearly.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Your editor uses a monospaced font, which this forum does not happen to like. If you like tabs, use tabs. If you like spaces, use spaces. Mixng them as indentation is like mixing C and C++ -- you generally make code hard to read, and annoy others who try to read your confuddled code.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    20
    Spaces and tabs were changed when I copied my code to the forum, it is and were easy to read in my editor.
    Here is a new version and I hope this time it will be easy to read.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    
    
    void func(char [],int ,char [],int );
    
    int main(void)
    {
    	
       char string1[50],string2[50];
        int c1,c2;
        
        printf("Your first string, enter '!' after entering your string to terminate :\n");
        scanf("%s",string1);
        
        printf("Your second string, add '!' again to the end of your string :\n");
        scanf("%s",string2);
        
        c1=strlen(string1)-1;
    	c2=strlen(string2)-1;
    	
    	printf("%s\n%s\n",string1,string2); // these two lines are just to check if array sizes to be passed to the function are right
    
    	printf("%d\n%d\n",c1,c2);
        
        func(string1,c1,string2,c2);
        
          
        
        return 0;
    }
    
    void func(char a[],int acount,char b[],int bcount){
         int j=0,i=0;
         
    	
         
    	 
    	 for(j=0;j<bcount;j++){
    
    		 if(strncmp(&b[j],a,acount)==0){
    
    			 if(b[j+acount]!='!'||b[j+acount]!='\0'){
    			 for(i=0;i<acount;i++){
    				 b[j]=b[j+acount];
    				 j++;
    			 }
    			 }
    			 if(b[j+acount]=='!'||b[j+acount]=='\0'){
    			 for(i=0;i<acount;i++){
    				 b[j]='\0';
    				 j++;
    			 }
    			 }
    		 }
    	 }
    
    
    	 printf("%s\n",b);
    
    }

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    This expersion will alway be true !!!
    Code:
    if( b[j+acount]!='!'||b[j+acount]!='\0')
    lets test with j = 0 and acount = 0, then we get to test
    Code:
    if ( b[0] != '!' || b[0] != '\0' )
    try to run this code :
    Code:
       char b[1];
    
       b[0] = '!';
       printf("b[0] = %c  b[0] != '!' || b[0] != '\\0' = %d\n",
               b[0], b[0] != '!' || b[0] != '\0');
       b[0] = '\0';
       printf("b[0] = %c  b[0] != '!' || b[0] != '\\0' = %d\n",
               b[0], b[0] != '!' || b[0] != '\0');
       b[0] = 'x';
       printf("b[0] = %c  b[0] != '!' || b[0] != '\\0' = %d\n",
               b[0], b[0] != '!' || b[0] != '\0');

    The results are :
    b[0] = ! b[0] != '!' || b[0] != '\0' = 1
    b[0] = b[0] != '!' || b[0] != '\0' = 1
    b[0] = x b[0] != '!' || b[0] != '\0' = 1

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    ... Why not use the already made 'strtok'

    Code:
    char str[] = "now # is the time for all # good men to come to the # aid of their country";
    char delims[] = "#";
    char *result = NULL;
    result = strtok( str, delims );
    while( result != NULL ) {
        printf( "result is \"%s\"\n", result );
        result = strtok( NULL, delims );
    }
    Result:
    Code:
      result is "now "
      result is " is the time for all "
      result is " good men to come to the "
      result is " aid of their country"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  2. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  3. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 12:08 AM
  4. Pointers and Strings
    By stallion in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 02:46 PM
  5. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 07:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21