Thread: problem with a function I'm trying to write

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    27

    problem with a function I'm trying to write

    I'm trying to write a function that inserts an 'x' where ever two consecutive ',' appears in a string. Here is my function I have below:

    Code:
     void insertx(char * string){
    	char prev;
    	char curr;
    	int i;
    	prev = 'x';
    	int j;
    	int length;
    	length = strlen(string);
    	for(i = 0;*(string + i) != '\0';i++){
    		curr = *(string + i);
    		//printf("curr:%c prev:%c\n",curr,prev);
    		//printf("test i = %d strlen = %d\n",i,strlen(string));
    		if((prev == ',') && (curr == ',')){
    			//printf("same!\n");
    			j = strlen(string);
    			while(j >= i){
    				*(string + j + 1) = *(string + j);
    				j--;
    			}
    			*(string + i) = 'x';
    			i = 0;
    			prev = 'x';
    		}
    		prev = curr;
    	}
    }
    This function works except for if a ',' occurs in the second character of a string.

    So lets say the string is 1,343,3423,343,, my function will modify the string as
    1x,343,3423,343,x,

    but I don't want that 1x in my string but I don't know how to remove this error.

    if anybody could see the problem in my code could you please point this out.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    immediately after setting prev to x inside your if statement you set it back to curr
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You seem to be doing more than you have to, i.e. calculating "length" that you never use.

    Unless you've been told you cannot use strstr() you're causing yourself a lot of trouble :-)

    Code:
    void insert_x(char * str)
    {
       char * from = str;
       char * copy = NULL;
       char tmp;
    
       while((from = strstr(from, ",,")) != NULL)
       {
          /* insert x between (from + 0) and (from + 1) */
    
          printf("found '%s'\n", from);
    
          /* copy to create a gap between from and from + 1 */
    
          *(from + 1) = 'x';
    
          from += 2;
       }
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    27
    oh wow I'm dumb, thanks a lot I've been wrestling with this problem for an hour now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM