Thread: Problem with strings >.<

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Question Problem with strings >.<

    Hey everyone, I'm kinda stuck in this: The purpose of the program is to crop a string between two given keywords. Example: if the keywords are "<dt>" and "</dt>" and the string is "<dt>TESTE</dt>" the program should print "TESTE".

    The prg is like this
    Code:
    int main(){
        char *string1="<dt>TESTE</dt>";
        char *string2;
        int inicio,final;
        inicio = searchString(string1,"<dt>");
        final = searchString(string1,"</dt>");
        cropString(string2,string1,inicio+4,final-1);
        printf("%s\n%s",string1,string2);
        return 0;
    }
    I've implemented a cropString(string2,string1,n1,n2) function that copies from string1[n1] until string2[n2] (included) and returns the given string in string2, and I guess it's working ok, but i think it doesnt put the '\0' at the end of my string.

    Code:
    void cropString(char* string2, char* string1,int n1,int n2){
        if ( (n1<0) || (n2<0) || (n2<n1) || (n1>strlen(string1)) || (n2>strlen(string1)) )
        {
            printf("Invalid arguments...");
            abort();
        }
    
    
        strncpy(string2,string1+n1,n2-n1+1);
        strcat(string2,"\n\n\n");
    }
    And the searchString(char* string1,char* keyWord)
    returns a integer value that represents where the string keyWord starts in string1 (returns -1 if not found or null keyWord)

    But when I call printf it shows me this:
    Problem with strings &gt;.&lt;-problkem-jpg

    The second line should be TESTE
    That seems like undefined behavior... Any1 knows what's happening?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe you have it! Put in the end of string char, and see what you get.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by Adak View Post
    I believe you have it! Put in the end of string char, and see what you get.
    That may sound a little noob but how do I put a '\0' at the end of a string? I've tried strcat(string2,"\0") but the '\0' goes to the end of that long string that appears on the prompt.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void cropString(char* string2, char* string1,int n1,int n2){
        if ( (n1<0) || (n2<0) || (n2<n1) || (n1>strlen(string1)) || (n2>strlen(string1)) )
        {
            printf("Invalid arguments...");
            abort();
        }
    
    
        strncpy(string2,string1+n1,n2-n1+1);
        strcat(string2,"\0");
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Hello everyone, I've tried this now:

    Code:
    int main(){
    
    
        char *string1="<dt>TESTE</dt>";
        char *string2 = malloc(55*sizeof(int));
        int ini,fin;
        ini = searchString(string1,"<dt>");
        fin = searchString(string1,"</dt>");
        cropString(string2,string1,ini+4,fin-1);
        printf("%s\n%s",string1,string2);
        return 0;
    }
    So that string2 could hold at least 55 chars without overlapping anyone's adress...
    the result was this:
    Problem with strings &gt;.&lt;-teste2-jpg

    I've noticed that those 2 last characters were changing everytime I compiled the program. Then I changed string1 to string literals of various lengths... the output was even worse when the substring between <dt> and </dt> had a length greater than 7:
    Problem with strings &gt;.&lt;-teste3-jpg

    That's undefined behavior for sure, so I must be writing or reading something off-bounds.

    My last shot was changing the declaration of string2 to:
    Code:
     char string2[55];
    and adding a final line in the cropString function:
    Code:
    string2[n2-n1+1]='\0';
    That worked great, and the output is as expected for any size of substring between <dt> and </dt>

    The big question is, what on earth happened in the other declarations of string2??

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm thinking you don't quite understand what a string is in C...

    C actually has no native string type and thus the compiler has absolutely no ability to deal with them. What we call "strings" in C are actually arrays of characters. Library functions both place and look for the \0 (char value of 0) at the end of the used portions of the char buffers to mark the end of the string. C library functions have no other means to know where the valid part of a string ends.

    It works like this...
    PHP Code:
    char string[10] = "Hello";

    memory...
    0 1 2 3 4 5 6 7 8 9
    H e l l o 0 
    $ ^ q 7 
    Now if that \0 isn't there you get what you got... the printf() (or whatever) function running on and on looking for it... In some cases I've seen the console screen scroll screen after screen after screen looking for that null... which might not even be there.

    So... in C a "string" is an array of characters with a sentinal value of 0 to indicate the end of the used portion.

    If you are manipulating strings, as you are, it falls to you --the programmer-- to put that nul terminator in there on your own.

    (See my suggestion in post #4)

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just a nit:
    Quote Originally Posted by h3now
    Hello everyone, I've tried this now:
    Code:
    char *string2 = malloc(55*sizeof(int));
    So that string2 could hold at least 55 chars without overlapping anyone's adress...
    That allocates memory for 55*sizeof(int)/sizeof(char) characters. Since an int is probably 4 or 8 bytes depending on your system, we are actually looking at space for 220 or 440 characters.





    Another nit:
    Code:
    char *string1="<dt>TESTE</dt>";
    That should be const char* as you are pointing to a string literal. Also, the second argument to cropString (the one for string1) should be const char* as well.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Problem with strings
    By fragrax in forum C Programming
    Replies: 6
    Last Post: 07-03-2008, 10:03 AM
  2. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  3. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  4. Problem with strings
    By PPhilly in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 06:56 PM
  5. please help me with Strings problem
    By suezq in forum C Programming
    Replies: 13
    Last Post: 04-03-2003, 08:38 PM

Tags for this Thread