copying the chars that are same

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

copying the chars that are same

 
0
  #1
Aug 5th, 2009
hello i have last problem with strings i dunno whats wrong with this i dont get sometimes the logic coz when i write the program i do it into smaller stuff but this rlly string stuff drive me insane i hope i m gonna master it in the end anyways i have this function that its kinda like strcmp but it should return address like if i have two strings for example
"thats","hats" then it should return the beging where both strings matched till and it should count the num of where it doesnt match now i m lost in my function anyway here the code so far it totally suck but i m lost in coding this i think i complicate things more ....
  1. #include <stdio.h>
  2. #include <string.h>
  3. int Check_Characters(char s1,char s2) {
  4. if(s1==s2)
  5. return 1;
  6. return 0;
  7. }
  8. char * String_In(char *str,char *str1) {
  9.  
  10. int counter=0;//how many characters we found diffrent
  11. int i;
  12. int x=0;
  13. char Temp[10];
  14. int num=0;//variable to know which string are bigger to make loop run according for that
  15. char *ptr;//used for returning will figure this out l8er
  16.  
  17. if(strlen(str) > strlen(str1))
  18. num=strlen(str);
  19. else
  20. num=strlen(str1);
  21. for(i=0; i< num ; i++) {
  22. //do a first test first but with changing num to i like if for example some char matched and some then didnt match num wont be = to i but this will solve this problem
  23. if(Check_Characters(str[i],str1[i])) {
  24. num=i;
  25. if(num==i){ //test first if num = to i to see if sequence of characters are =
  26. num++;
  27. puts("Dude it being runned");
  28. Temp[x]=str[i];
  29. i++;
  30. }
  31. }
  32. }
  33. puts(Temp);//to see if temp output
  34. }
  35. int main(void)
  36. {
  37. int x;
  38. char temp[]="thats";
  39. char temp2[]="hats";
  40. String_In(temp,temp2);
  41. return 0;
  42. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: copying the chars that are same

 
0
  #2
Aug 5th, 2009
Please don't use SMS like language, its making your problem tough to understand.
Now tell, from above program what are the outputs you are expecting.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: copying the chars that are same

 
1
  #3
Aug 5th, 2009
1. Why create this function when you can simply compare two characters with operator ==.
int Check_Characters(char s1,char s2) {
    if(s1==s2)
        return 1;
    return 0;
}

2. You have made same mistake again like your last post. First, you assign num = i so that also mean num is equal to i . Then, you compare num and i which they will always equal to each other.
            num=i;
            if(num==i)

3. x is always 0
Temp[x]=str[i];
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: copying the chars that are same

 
0
  #4
Aug 5th, 2009
oh yh i was kinda sleepy yesterday i can do for(i=0;str[i]!=0 && str1[x]!=0 && str[i]==str1[x],i++,x++) but that wont get all characters coz i wanna extract all strings that match in both strings for and put not matched one in a ptr like for example "thats my dog","that is my cat"; it will change first strings that is my and get dogcat in a ptr forum but i seriously dunno how to do this ...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: copying the chars that are same

 
0
  #5
Aug 6th, 2009
i have made some code but i stops after first string it it doesnt chk the others
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. char * String_Within(char *str1,char *str2) {
  4.  
  5. char temp[50];
  6. int i,x=0;
  7. int z=0;
  8. int num=0;
  9. /*first of all lets run in a double for loop and each time will be less than the certain num */
  10. /*that certain num will have to know when the space is there and 2nd time is run we += it so it doesnt stop at that space again */
  11. //first of all lets read in nested loop
  12. for( i=x ; str1[i]!=0 ; i++ ) {
  13. for( x=num ; str2[x]!=0; x++ ) {
  14. num++;
  15. //we will keep incrementing num
  16. if(str2[x]==str1[i]) {
  17. temp[z]=str1[i];
  18. z++;
  19. }
  20. if(isspace(str2[x])) {
  21. num+=1;//start at the stuff after the space
  22. break;
  23. }
  24. else
  25. break;
  26. }
  27. temp[z]=' ';//add a space to the character we have atm
  28. }
  29. temp[z]='\0';//to put null character
  30. puts(temp);
  31. //we gonna return it later now
  32. }
  33. int main(void)
  34. {
  35. char Name[]="thats good a coder";
  36. char Name1[]="thats good a dog";
  37. char *ptr;
  38. ptr=String_Within(Name,Name1);
  39. //will figure out rest of code later
  40. return 0;
  41. }
it only got thats then a O .... and it should be counting the 2nd string in the char which is good which is right too ... once i solved this i guess then i can easily compare temp to str and extract the chars that arent same and return it to main function and change str to temp
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: copying the chars that are same

 
0
  #6
Aug 6th, 2009
hmm i had a lot of problems in that code i changed that but now problem is left that sometimes the 1st chars has more char than other so it wont test it even if the 3rd string are == there will be one char that surpasses the other this is i think there must be easier way to do this than the way i m doing ....
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. char * String_Within(char *str1,char *str2) {
  4.  
  5. char temp[50];
  6. int i,x=0;
  7. int z=0;
  8. int num=0;
  9. int buff=1;
  10. /*first of all lets run in a double for loop and each time will be less than the certain num */
  11. /*that certain num will have to know when the space is there and 2nd time is run we += it so it doesnt stop at that space again */
  12. //first of all lets read in nested loop
  13. for( i=num ; str1[i]!=0 ; i++ ) {
  14. for( x=num ; x<buff; x++ ) {
  15. printf("\nhere str[i] %c and here str[x] %c\n",str1[i],str2[x]);
  16. getchar();
  17. if(str2[x]==str1[i]) {
  18. temp[z]=str1[i];
  19. z++;
  20. }
  21. if(isspace(str2[x]) && isspace(str1[i])) {
  22. num++;
  23. break;
  24. }
  25. else if(str1[i]!=str2[x]) {
  26. num++;
  27. printf("\nsorry characters here are they %c %c\n",str1[i],str2[x]);
  28. break;
  29. }
  30. //we will keep incrementing num
  31. num++;
  32. }
  33. buff+=1;
  34. temp[z]=' ';//add a space to the character we have atm
  35. }
  36. temp[z]='\0';//to put null character
  37. puts("now we are printing temp\n");
  38. puts(temp);
  39. //we gonna return it later now
  40. }
  41. int main(void)
  42. {
  43. char Name[]="thats a good dog";
  44. char Name1[]="thats a good coder";
  45. char *ptr;
  46. ptr=String_Within(Name,Name1);
  47. //will figure out rest of code later
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: copying the chars that are same

 
0
  #7
Aug 6th, 2009
To be honest, it is hard to read your code without know the clear purpose of what your function intent to do. It would be easy if you tell us what will your function return, what each parameters do, and what is your expected result. By telling us these pieces of information, it save us 30 minutes more in analyze your whole code.
Last edited by invisal; Aug 6th, 2009 at 11:04 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: copying the chars that are same

 
0
  #8
Aug 7th, 2009
well it should extract the strings to are equal to first char * and return the string that are diffrence like for example this is cool this is bad it will return cool bad and change first string to this is
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 139
Reputation: Dream2code is an unknown quantity at this point 
Solved Threads: 11
Dream2code's Avatar
Dream2code Dream2code is offline Offline
Junior Poster

Re: copying the chars that are same

 
0
  #9
Aug 7th, 2009
Why dont you use strstr() fucntion to find the beginning address of the match then get the string length of the 2nd sting.
Last edited by Dream2code; Aug 7th, 2009 at 5:56 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: copying the chars that are same

 
0
  #10
Aug 7th, 2009
yah but i dont wanna use any standard function i wanna do it myself
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC