Thread: Basic help with strings

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    90

    Basic help with strings

    Hello gurus,

    I still have problems with C learning so please if somebody want to fix my concrete problem:

    Code:
    // here I search for string under bigger string
    
             char* found = strstr(bigString, searchPart);
             if (found)
                 {
                 
                 char* leftpart = ?
                 char* midpart = ?
                 char* rightpart = ?
    
                 gchar* markup;
                 markup = g_markup_printf_escaped("%s<b>%s</b>%s", leftpart, midpart, rightpart);
    
                 etc...
    I dont know to get 'leftpart', 'midpart' and 'rightpart' based on 'found' which I need for fill into gtktreeview with markup.

    So, please help.
    I prefer ready, clean and fast solution
    Thank you, nime.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Where does found point? Can you use that as one of your three?

    Where does right begin? At the end of your found string? Then you'll have to advance from the beginning to the end (by the length of the substring).

    I would bet left begins at the beginning of the original string.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Well tabstop,
    If I understand well strstr should return place where finded string begins.
    So, left part should be from 1 to found.
    Mid part from found to found + strlen(searchPart)
    And right part from found + strlen(searchPart) + 1 to strlen(bigString)

    In visial basic I can write this in 5 seconds, but in C I can't.
    So I ask someone to do this for me.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nime View Post
    Well tabstop,
    If I understand well strstr should return place where finded string begins.
    So, left part should be from 1 to found.
    Mid part from found to found + strlen(searchPart)
    And right part from found + strlen(searchPart) + 1 to strlen(bigString)

    In visial basic I can write this in 5 seconds, but in C I can't.
    So I ask someone to do this for me.
    I don't know why you say you can't in C, because you just did. (Once you realize that in C your char pointers point to the start of the string.)

    EDIT: Now, if you need them to be honest C-strings, with a \0 at the end, then you will need to acquire more memory to put these in, you can't do them in place, because the \0 will overwrite part of your strings.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So if I understand you correctly, say your string was "abcdefg", and your search string was "de". That means you want leftpart to be "abc", midpart to be "de" and rightpart to be "fg"? If so, you can do so with
    Code:
    leftpart = bigString;  // point to the first char of bigString (index 0)
    midpart = found;  // of course, the mid part is where i found searchPart
    rightpart = found + strlen(searchPart) + 1;  // point to the first char after found, there will be a null terminator at the end of bigString to stop me
    That should make them each point to the respective parts of bigString. Now, to get this printed into your markup string correctly. Then, look into using "%.*s" modifier. The . means you're giving a maximum width, and the * means that you insert an extra parameter that specifies how many characters you want to print, max. The man page for printf will give you more details. You can find the length of a part by subtracting the pointers, like midpart-leftpart.
    Last edited by anduril462; 06-30-2011 at 02:35 PM. Reason: fixed format string suggestion to include dot

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    anduril,
    thanks for help but this is not exactly what I need.

    Shown part of program is inside for loop and 'found' changes often same as length of my parts.
    I tested my markup string "by hand" and this part works OK.
    I also don't understand pointers well so...
    Here would be the best if someone write asked code for me.
    This is only detail which remains to make my test program with gtktreeview - finished.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nime View Post
    anduril,
    thanks for help but this is not exactly what I need.
    You're welcome. I know it's not a "drop in place" solution, but it should give you some ideas as to how to make your program work.

    Shown part of program is inside for loop and 'found' changes often same as length of my parts.
    So each time through the loop, you will have a different bigString, searchPart and found. Each time you will also calculate a new leftpart, midpart and rightpart, so this shouldn't be a problem.
    I tested my markup string "by hand" and this part works OK.
    That's great. We know it's the logic of determining left, mid and right that you need help with, and help you we did.
    I also don't understand pointers well so...
    So what? Go learn them. It will be good for you. We have a tutorial on this very site, here. Google will turn up tons more. Go get your learn on.
    Here would be the best if someone write asked code for me.
    No, it would not be best, because next time you have a pointer problem, you will just have to ask somebody else to write code for you again. Best would be you learning how to program C and how to use pointers. Then you can write it yourself, and we can help with the little bumps in the road.
    This is only detail which remains to make my test program with gtktreeview - finished.
    Well, guess you better get going, so you can finish your program.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Why dont you shown an example on what is the right, left, and the mid part your thinking in your mind. With an example string, for which we could propose a solution?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    ssharish2005

    I didn't show more code because it is complex and I make one minimal example for try to get what I need what was not good idea.
    Strings are readen from structures in binary file and here could be few thousand of strings.
    Strings on disk are written with dos codepage to 256 chars and then converted with iconv to UTF8 to be suitable for showing with gtk+ and much, much more.

    Anyway,
    Thanks for all which try to help.
    Regards, nime.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So are you just trying to point to those spots in a string, or are you actually trying to copy them to their own buffers? If the former:
    Code:
    char *l = wholestring;
    char *m = strstr( wholestring, "findthis" );
    char *r = m ? m + strlen( findthis );
    If the latter, do almost the same thing, except start copying blocks to their individual buffers.

    Give it a try. Don't worry about doing it in a loop first. Just try to get what you want working with a single string that you pass it. Then worry about putting it into a bigger program.


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

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A very basic program to give an example to demonstrate what has been talked about:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(void){
    
    	char *myString = "LeftMiddleRight";
    	char *searchString = "Middle";
    	char *mySearch, Left, Right, Middle;
    
    	mySearch = strstr(myString, searchString);
    	if(mySearch){
    		Left = myString[0];
    		Middle = *mySearch;
    		Right = *(mySearch + strlen(searchString));
    		printf("%c %c %c", Left, Middle, Right);
    		printf("\n%s\n%s\n%s\n", myString, mySearch,(mySearch + strlen(searchString))); 
    	}
    	
    	_getch();
    	return 0;
    }
    Last edited by AndrewHunter; 06-30-2011 at 04:25 PM. Reason: Fixed Spacing
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    A very basic program to give an example to demonstrate what has been talked about:
    Code:
    #include <stdio.h>
    #include <conio.h>
    ...
    	_getch();
    	return 0;
    }
    Seems silly to include a separate header just for one function, when similar functionality exists in stdio.h.


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

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    Seems silly to include a separate header just for one function, when similar functionality exists in stdio.h.
    Quzah.
    Quite a good point, thanks Quzah. That's what I get for just leaving a bare framework in place for these cboard posts.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Andrew,
    I can't get to work your example.
    And I found why I can't solve this problem.

    char* found = strstr("abcdefg", "de"); gives a pointer and I need integer.
    In this case my integer should be 4.

    I try to cast (int)found but without results (I get some address).
    Now is question, how can I get 4 from found in this example?

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You actually want 3 since arrays begin indexing at 0. C style strings are char arrays. So in your example char myString[]="abcdefg"; produces an array with 7 elements which are indexed 0 - 6. So for the position of d it would be myString[3]. An example of this is:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(void){
    
    	char myString [] = "abcdefg";
    	char *searchString = "de";
    	char *mySearch;
    	int arrayOffset;
    
    	mySearch = strstr(myString, searchString);
    	if(mySearch){
    		arrayOffset = (mySearch - myString);
    		printf("%d %c", arrayOffset,myString[arrayOffset]);
    	}
    	
    	_getch();
    	return 0;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  3. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM