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 ....

#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;
}

Recommended Answers

All 18 Replies

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.

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];

oh yh i was kinda sleepy yesterday i can do for(i=0;str!=0 && str1[x]!=0 && str==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

#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;
}

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

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 ....

#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.

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

Why dont you use strstr() fucntion to find the beginning address of the match then get the string length of the 2nd sting.

yah but i dont wanna use any standard function i wanna do it myself

DO one thing...mention clearly
input to your program
Expected output and

where you facing the problem..no need to mention code again just explain to the point.

well i did mention expected output the problem in my program here that one string after the space can be larger than the other so if we got a 3rd string even if they match it will say it wont match because he will be i+1 comparing to x so it wont match if somehow i could just strlen the char after space if there the same then i should apply an if test ...

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

if the case is like

"this is cool man"
" is is ool man"

what is your expected O/P here..generalize the thing.

first string will change to is man
and return this cool ool

first string will change to is man
and return this cool ool

r u comparing word by word ..??

i guess u r taking the whole stings as set of chars not by word..ryt?

yah comparing word by word i got one idea but i dunno how to do first i will count all spaces in both if there is 1 it will only do strcmp between both if there 2 or 3 it will first somehow like first word if its count greater than 1st word in 2nd string it will ignore that and put that inside temp which we will return l8er if its the same it should compare them but the temp wont be in the thing so if it doesnt find the chars not = it wont put that in temp .......

1)At first design a prototype of your function like

its input parameters
return value
and its usage.

2)Then set few examples , i mean show
for these i/p's o/p shold be like this.specify some cases..
give:5 differnet kinda examples on how your function should behave.
Then only we can generalize the thing to work with.

okay i think how i will do it i desinged the function etc but now the problem is its getting in for loop first i knew how to change first the strings that are = and after that when i copy strings that are = to a temporily array then after that i will compare both to largest str and smallest str with the temporirly and get the strings that arent =

now problem is that my function get in a forever loop even though i made test inside once stuff are done it will set x to 10 which will break out of the loop !!!! this stupid function driving me insane

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void ParseSpaces(char *str,char *str2,int *space1,int *space2,int *str1count,int *str2count) {
    int i,x;
    *space1=*space2=0;//set both to 0 so we dont be incrementing addresses
    *str1count=*str2count=0;//set both to 0 so we dont be incrementing addresses
    while( *str!=0 ) {
        ++(*str1count);
        if(*str==' ')
            ++(*space1);
        str++;
    }
    while(*str2!=0) {
        ++(*str2count);
        if(*str2==' ')
            ++(*space2);
        str2++;
    }
}

void FixStr(char *str1,char *str2,char *biggest,char *smallest) {//stands for parse biggest string
    int num;
    if(strlen(str1) > strlen(str2) ) {
        for(num=0;str1[num]!=0;num++)
            biggest[num]=str1[num];//keep setting temp[num] to str1[num
        biggest[num]='\0';//set to NULL character

        //now we gonna set the smallest buffer
        for(num=0;str2[num]!=0;num++)
            smallest[num]=str2[num];
        smallest[num]='\0';//set to NULL character
    }
    else {
        for(num=0;str2[num]!=0;num++)
            biggest[num]=str2[num];
        biggest[num]='\0';

         for(num=0;str1[num]!=0;num++)
            smallest[num]=str1[num];
        smallest[num]='\0';//set to NULL character
    }
}
char * String_Within(char *str1,char *str2) {
    int Sspaces1,Sspace2,str1count,str2count;
    int num;//to find the biggest count and run loop acccording to it
    int Tracker=0;//to keep track of the String
    char Biggest[50];//holds biggest array
    char Smallest[50];//holds smallest array
    char tempp[50];//which will get the strings that are ==
    int chars1=0;//will keep track of chars if there == then we will do some for loop stuff in the while
    int chars2=0;//will keep track of chars if there == then we will do some for loop stuff in the while
    int Tr=0,Tr2=0;//keep track of certain variables
    int x=0;//which we will use to make loop run till we fix str
    int counter=0;//certain variable in our for loop
    int temporlily=0;//to increment this variable so if same as the chars we copy the stuff
    int i;
    ParseSpaces(str1,str2,&Sspaces1,&Sspace2,&str1count,&str2count);//get the spaces and how much characters inside we will use that l8er on
    if(str1count<str2count)// set num to the smallest count of the 2 
        num=str1count;
    else
        num=str2count;
    FixStr(str1,str2,Biggest,Smallest);//we gonna parse biggest string to temp
    while( x<9 ) {//just a loop where i could stop it easily if that needs to repeat till i get all string that are =
        for( i=Tracker ; i<num; i++) {
            Tracker++;//keep incrementing it so we start with that variable array[i] next time loop runs
            chars1++;
            chars2++;
            if(isspace(Smallest[i]) || isspace(Biggest[i]))
                break;
        }
        if(chars1==chars2) {
            for( counter=Tr ;counter < Tracker ; counter++) {
                if(Biggest[counter]==Smallest[counter])
                    temporlily++;
                Tr++;
            }
            if(temporlily==chars1) {//then here if they are = then it will be same count
                for(counter=Tr2; counter < Tracker ; counter++) {
                    tempp[counter]=Biggest[counter];
                    Tr2++;
                }
                tempp[counter]=' ';
                counter++;
                tempp[counter]='\0';//will be overwritten l8er on
            }
        }
        if(i==num-1)//I test if i == to num-1 then that means we reached our end
            x=10;//will stop loop now
    }
    puts(tempp);
}
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;
}

i think i had some bugg but now its still in whilever loop dam this gonna drive me insane

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void ParseSpaces(char *str,char *str2,int *space1,int *space2,int *str1count,int *str2count) {
    int i,x;
    *space1=*space2=0;//set both to 0 so we dont be incrementing addresses
    *str1count=*str2count=0;//set both to 0 so we dont be incrementing addresses
    while( *str!=0 ) {
        ++(*str1count);
        if(*str==' ')
            ++(*space1);
        str++;
    }
    while(*str2!=0) {
        ++(*str2count);
        if(*str2==' ')
            ++(*space2);
        str2++;
    }
}

void FixStr(char *str1,char *str2,char *biggest,char *smallest) {//stands for parse biggest string
    int num;
    if(strlen(str1) > strlen(str2) ) {
        for(num=0;str1[num]!=0;num++)
            biggest[num]=str1[num];//keep setting temp[num] to str1[num
        biggest[num]='\0';//set to NULL character

        //now we gonna set the smallest buffer
        for(num=0;str2[num]!=0;num++)
            smallest[num]=str2[num];
        smallest[num]='\0';//set to NULL character
    }
    else {
        for(num=0;str2[num]!=0;num++)
            biggest[num]=str2[num];
        biggest[num]='\0';

         for(num=0;str1[num]!=0;num++)
            smallest[num]=str1[num];
        smallest[num]='\0';//set to NULL character
    }
}
char * String_Within(char *str1,char *str2) {
    int Sspaces1,Sspace2,str1count,str2count;
    int num;//to find the biggest count and run loop acccording to it
    int Tracker=0;//to keep track of the String
    char Biggest[50];//holds biggest array
    char Smallest[50];//holds smallest array
    char tempp[50];//which will get the strings that are ==
    int chars1=0;//will keep track of chars if there == then we will do some for loop stuff in the while
    int chars2=0;//will keep track of chars if there == then we will do some for loop stuff in the while
    int Tr=0,Tr2=0;//keep track of certain variables
    int x=0;//which we will use to make loop run till we fix str
    int counter=0;//certain variable in our for loop
    int temporlily=0;//to increment this variable so if same as the chars we copy the stuff
    int i;
    ParseSpaces(str1,str2,&Sspaces1,&Sspace2,&str1count,&str2count);//get the spaces and how much characters inside we will use that l8er on
    if( str1count<str2count )// set num to the smallest count of the 2
        num=str1count;
    else
        num=str2count;
    FixStr(str1,str2,Biggest,Smallest);//we gonna parse biggest string to temp
    while( x<9 ) {//just a loop where i could stop it easily if that needs to repeat till i get all string that are =
        for( i=Tracker ; i<num; i++) {
            Tracker++; //keep incrementing it so we start with that variable array[i] next time loop runs
            chars1++;
            chars2++;
            if(isspace(Smallest[i]) || isspace(Biggest[i]))
                break;
        }
        if(chars1==chars2) {
            for( counter=Tr ;counter < Tracker ; counter++) {
                if(Biggest[counter]==Smallest[counter])
                    temporlily++;
                Tr++;
            }
                if(temporlily==chars1) {//then here if they are = then it will be same count
                    for(counter=Tr2; counter < Tracker ; counter++) {
                        tempp[counter]=Biggest[counter];
                        Tr2++;
                    }
                    tempp[counter]=' ';
                    counter++;
                    tempp[counter]='\0';//will be overwritten l8er on
                }
            chars1=chars2=0;//set back to 0 so we l8er do test again while our loop runs
            }
        if( i==num-1 )//I test if i == to num-1 then that means we reached our end
            x=10;//will stop loop now
    }
    puts(tempp);
}
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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.