Thread: my strcmp and strrev own function

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    Post my strcmp and strrev own function

    hi
    i need to write two program such as strcmp and strrev functions
    please help me
    best regards

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you done so far?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    for strcmp :
    Code:
    #include <stdio.h>
    void main()
    {
     char a[10],b[10];
     int i=0,t=1;
     gets(a);
     gets(b);
     while(a[i]!='\0')
     {
       if(a[i]!=b[i])t=0;
       i++;
     }
     if(t==1)
    printf("equal");
     else printf("no equal");
    }
    Last edited by irwebnews; 05-18-2012 at 04:19 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why did you make printf display the same result no matter what actually happened?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Why did you make printf display the same result no matter what actually happened?
    edited
    but this program is wrong...
    i need correct program for strcmp function...

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The alphabet is ordered such that a is less than b. You'll basically have to do other relational tests after you determine that the two current characters are unequal, since you want a function exactly like strcmp.

    Also read these.
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Try to write strrev before you ask for more help. I will give you a big hint. You can reverse a string in place by swapping certain characters.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    5
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     char a[200],b[200];
     int i=0,t=1;
     printf("enter frist string=");
     gets(a);
     printf("\n enter second string=");
     gets(b);
     while(a[i]||b[i]!='\0')
     {
       if(a[i]!=b[i])t=0;
       i++;
     }
     if(t==1)
    printf("equal");
     else printf("no equal");
    
    
    getch();
    }

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I am going to append this to my signature, because it seems like it comes up every day now:

    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead. If you don't know how, Google it.

    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.

    3. Get rid of conio.h and other antiquated DOS crap headers that haven't been around since the early 90s. If you want to use "getch()" use the standard getchar() instead.

    Only after all of this is fixed, should you start thinking about your program. Otherwise you are pretty much starting your drive with the handbrake on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    5
    strrev:

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     char str[50],revstr[50];
     int len=0,i,j=0;
     printf("This program is to reverse a string");
     printf("\n enter the string to be reversed : ");
     gets(str);
     while(str[len])
         len++;
     for(i=len-1;i>=0;i--)
       {
         revstr[j]=str[i];
         j++;
       }
     revstr[j]='\0';
     clrscr();
     printf("input string : %s",str);
     printf("\n inverted string : %s",revstr);
     getch();
    }

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I see that you've took Claudiu's advice to heart.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by memcpy View Post
    I see that you've took Claudiu's advice to heart.
    Yes, looks like we have another one with communication difficulties that just barfs the code from his editor here without any questions, explanations or requests.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with write your own strcmp function
    By Iris Li in forum C Programming
    Replies: 1
    Last Post: 05-03-2012, 08:47 AM
  2. string reversing function strrev() errors
    By suryak in forum C Programming
    Replies: 4
    Last Post: 06-18-2011, 07:54 AM
  3. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  4. reverse string without using strrev() function
    By revolution3396 in forum C Programming
    Replies: 3
    Last Post: 11-27-2008, 02:34 AM
  5. trying not to use strrev() function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-21-2001, 03:05 PM