| | |
copying the chars that are same
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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 ....
"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 ....
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int Check_Characters(char s1,char s2) { if(s1==s2) return 1; return 0; } char * String_In(char *str,char *str1) { int counter=0;//how many characters we found diffrent int i; int x=0; char Temp[10]; int num=0;//variable to know which string are bigger to make loop run according for that char *ptr;//used for returning will figure this out l8er if(strlen(str) > strlen(str1)) num=strlen(str); else num=strlen(str1); for(i=0; i< num ; i++) { //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 if(Check_Characters(str[i],str1[i])) { num=i; if(num==i){ //test first if num = to i to see if sequence of characters are = num++; puts("Dude it being runned"); Temp[x]=str[i]; i++; } } } puts(Temp);//to see if temp output } int main(void) { int x; char temp[]="thats"; char temp2[]="hats"; String_In(temp,temp2); return 0; }
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.
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/
Indian Developer
http://falaque.wordpress.com/
1. Why create this function when you can simply compare two characters with operator ==.
2. You have made same mistake again like your last post. First, you assign
3.
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 0Temp[x]=str[i]; Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
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 ...
i have made some code but i stops after first string it it doesnt chk the others
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> char * String_Within(char *str1,char *str2) { char temp[50]; int i,x=0; int z=0; int num=0; /*first of all lets run in a double for loop and each time will be less than the certain num */ /*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 */ //first of all lets read in nested loop for( i=x ; str1[i]!=0 ; i++ ) { for( x=num ; str2[x]!=0; x++ ) { num++; //we will keep incrementing num if(str2[x]==str1[i]) { temp[z]=str1[i]; z++; } if(isspace(str2[x])) { num+=1;//start at the stuff after the space break; } else break; } temp[z]=' ';//add a space to the character we have atm } temp[z]='\0';//to put null character puts(temp); //we gonna return it later now } int main(void) { char Name[]="thats good a coder"; char Name1[]="thats good a dog"; char *ptr; ptr=String_Within(Name,Name1); //will figure out rest of code later return 0; }
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 ....
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> char * String_Within(char *str1,char *str2) { char temp[50]; int i,x=0; int z=0; int num=0; int buff=1; /*first of all lets run in a double for loop and each time will be less than the certain num */ /*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 */ //first of all lets read in nested loop for( i=num ; str1[i]!=0 ; i++ ) { for( x=num ; x<buff; x++ ) { printf("\nhere str[i] %c and here str[x] %c\n",str1[i],str2[x]); getchar(); if(str2[x]==str1[i]) { temp[z]=str1[i]; z++; } if(isspace(str2[x]) && isspace(str1[i])) { num++; break; } else if(str1[i]!=str2[x]) { num++; printf("\nsorry characters here are they %c %c\n",str1[i],str2[x]); break; } //we will keep incrementing num num++; } buff+=1; temp[z]=' ';//add a space to the character we have atm } temp[z]='\0';//to put null character puts("now we are printing temp\n"); puts(temp); //we gonna return it later now } int main(void) { char Name[]="thats a good dog"; char Name1[]="thats a good coder"; char *ptr; ptr=String_Within(Name,Name1); //will figure out rest of code later return 0; }
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
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
- Copying a microsoft word doc (C++)
- How to avoid NULL Terminator in MY-String class (C++)
- Help for comparing strings and copying strings please. (C++)
- End of an Era? DVD copying threatened. (Geeks' Lounge)
- JProgressBar while file copying (Java)
- shallow copying (Java)
- lowercase chars (C)
Other Threads in the C Forum
- Previous Thread: Associativity of operators in c
- Next Thread: Reading EBCDIC file through C
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi





