hi
i need to write two program such as strcmp and strrev functions
please help me
best regards
hi
i need to write two program such as strcmp and strrev functions
please help me
best regards
What have you done so far?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Why did you make printf display the same result no matter what actually happened?
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.
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(); }
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.
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(); }
I see that you've took Claudiu's advice to heart.
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.