#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    6
    Rep Power
    0

    Few basic c questions...


    Hey, I wondered if anyone could help me with a few basic c questions?

    I wondered if its possible to do calculations in a printf command with a function... e.g.

    printf( "%f\n", power(4/100,12) ) ;

    Odd request, as you'd usually just calculate 4/100 and put that value in.. but I wondered if it's possible...

    Also, is it possible to delete a char from a string?

    Say you have the string 'been' and wanted to 'delete' one of the e's... How would you go about doing this?

    Thanks in advance for any help you can give!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Posts
    580
    Rep Power
    97
    Originally Posted by AmazingFish
    I wondered if its possible to do calculations in a printf command with a function!
    Shure.
    Also, is it possible to delete a char from a string?
    If it's a std::string use string::erase().
    If it's a char array you have to create your own function.
    Kurt
  4. #3
  5. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Originally Posted by AmazingFish
    Also, is it possible to delete a char from a string?

    Say you have the string 'been' and wanted to 'delete' one of the e's... How would you go about doing this?

    Thanks in advance for any help you can give!
    Here's an example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int i;
    	char *myString = "been";
    
    	printf("Before: %s\n", myString);
    
    	for (i = 2; i < (int)strlen(myString); i++) {
    		myString[i] = myString[i + 1];
    	}
    
    	printf("After: %s\n", myString);
    
    	return(0);
    }
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    This is a bad idiom to use:
    Code:
    for (i = 2; i < (int)strlen(myString); i++) {
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  8. #5
  9. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Originally Posted by Dave Sinkula
    This is a bad idiom to use:
    Code:
    for (i = 2; i < (int)strlen(myString); i++) {
    Do you mean it's bad to use it within the for loop or that it's bad in general?
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Originally Posted by mvantuyl
    Do you mean it's bad to use it within the for loop or that it's bad in general?
    Yes, as the condition in a for loop.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  12. #7
  13. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Originally Posted by Dave Sinkula
    Yes, as the condition in a for loop.
    OK. I realize that it's not a good thing to call a function within the for loop, but got lazy and did it the easy (ineffecient) way. Is this better?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int i, length;
    	char *myString = "been";
    
    	printf("Before: %s\n", myString);
    
    	length = (int)strlen(myString);
    
    	for (i = 2; i < length; i++) {
    		myString[i] = myString[i + 1];
    	}
    
    	printf("After: %s\n", myString);
    
    	return(0);
    }
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  14. #8
  15. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Or better yet:
    Code:
    length = (int)strlen(myString);
    
    memcpy(myString + 2, myString + 3, length - 2);
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Oh, I should have mentioned this too:
    Code:
    char *myString = "been";
    String literals should be considered read-only. Attempting to write to one is UB.

    And regarding the memcpy, it is UB for overlapping arrays. Instead use memmove.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    So taking some time to offer something other than nit-picks...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    static const char Text[] = "abcdefg";
    
    char *delchar(char *s, size_t n)
    {
       char *dest = &s[n + 1];
       memmove(&s[n], dest, strlen(dest) + 1/* include terminating null */);
       return s;
    }
    
    void test(size_t n)
    {
       char text[sizeof Text];
       strcpy(text, Text);
       printf("delchar(\"%s\", %lu) =", text, (long unsigned)n);
       printf("\"%s\"\n", delchar(text, n));
    }
    
    int main(void)
    {
       size_t i;
       for ( i = 0; Text[i] != '\0'; ++i )
       {
          test(i);
       }
       return 0;
    }
    
    /* my output
    delchar("abcdefg", 0) ="bcdefg"
    delchar("abcdefg", 1) ="acdefg"
    delchar("abcdefg", 2) ="abdefg"
    delchar("abcdefg", 3) ="abcefg"
    delchar("abcdefg", 4) ="abcdfg"
    delchar("abcdefg", 5) ="abcdeg"
    delchar("abcdefg", 6) ="abcdef"
    */

    Comments on this post

    • mvantuyl agrees : A vast improvement over mine. :)
    • medialint agrees : Cheers for your effort and advice
    • codergeek42 agrees
    • adorablepuppy agrees : rep++ for being a helpful guy.
    • Axweildr agrees
    • LinuxPenguin agrees : You are the master
    • medianox agrees : Lord of all that is kludge.... or remotely kludge ;)
    • ishnid agrees
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    9
    Rep Power
    0
    How can i replace certain character in a string .

    Like

    string="the is a cat";

    where ever i find character "a" i need to replace it with "o".

    How can i do it in C ?

    please help in this regard.

    trinath Somanchi
  22. #12
  23. Making a mistake is not sin.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Finland
    Posts
    406
    Rep Power
    209
    At least by going through every char in a array, and checking if char is a. If condition is true, just replace it.

    Code:
    int len,index;
    char *array;
    .
    .
    .
    len=strlen(array);
    for(index=0;index<len;index++)
    {
        if(array[index]=='a')
        {
            array[index]='o';
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo