MrNoob 24 Posting Whiz in Training

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

MrNoob 24 Posting Whiz in Training

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

MrNoob 24 Posting Whiz in Training

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 …
MrNoob 24 Posting Whiz in Training

anyways thanks case solved :P

MrNoob 24 Posting Whiz in Training

i think it can be with VLA variables

MrNoob 24 Posting Whiz in Training

thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?

MrNoob 24 Posting Whiz in Training

Hey its me again i dunno this string stuff gonna drive me insane really been doing since yesterday exercises on string did half of them but i have this program which should remove chars that i specify and when i test with putchar it does remove them but The string itself didnt !!!

#include <stdio.h>
#include <string.h>
int RemoveChars(char *S,char c) {
    int i=0;
    int spaces=0;
    char temp;
    for(i=0;S[i]!=0;i++) {
        temp=S[i];
        if(S[i]!=c) {
            S[i]=temp;
            putchar(temp);//this totally works !!!!!!
        }
        else
            spaces++;
    }
    return spaces;
}
int main(void)
{
    char name[]="dude and duder";
    int spaces;
    spaces=RemoveChars(name,' ');
    printf("Now after its Changed its %s and there was %d spaces\n",name,spaces);//What the Hell why doesnt it WORK !!!!!!!
    return 0;
}
MrNoob 24 Posting Whiz in Training

oh thanks alot man nice info for that char ** didnt know abt it thanks guys for your help again don't know what i have done without u.

MrNoob 24 Posting Whiz in Training

wow but why though even though its a character ptr why need char pointer to a pointer since 1 pointer can hold the address of a string ?

MrNoob 24 Posting Whiz in Training

alright it worked now but i got now one problem left that end doesnt specify anything

#include <stdio.h>
#include <string.h>

int ParseCh(char *S,char *buff,int num,char *End) {
    int i,temp=0;
    for(i=0;S[i]!=0;i++) {
        if(S[i]==',' || S[i]=='\n' || S[i]=='\t' || i==num) {
            puts("found it");
            temp=1;
            End=&S[i]+1;
            S[i]=0;
            break;
        }
    }
    if(temp)
        strcpy(buff,S);
    else {
        End=0;
        i=0;
    }
    return i;
}
int main(void)
{
    char Name2[]="NAME,M";
    char Name[10]={0};
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh(Name2,Name,5,End);
    printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}
MrNoob 24 Posting Whiz in Training

ohhhhh

MrNoob 24 Posting Whiz in Training

thanks man i changed that but i still get runtime error

MrNoob 24 Posting Whiz in Training

why are you using nested loop why dont u first fill the array with rand and make another temp array to hold the variables with same size and
do another for loop to chk if that array==temp if its = then just rand() and its better to use srand(GetTickCount()); produces much random result i think ..

MrNoob 24 Posting Whiz in Training

now i have my other function which i get runtime error on i don't why though

#include <stdio.h>
#include <string.h>
int ParseCh(char *S,char *buff,int num,char *End) {
    int i,temp;
    for(i=0;S[i]!=0;i++) {
        if(S[i]==',' || S[i]=='\n' || S[i]=='\t' || i==num) {
            temp=1;
            End=S+1;
            S[i]='\0';
            break;
        }
    }
    if(temp)
        strcpy(buff,S);
    else {
        End=0;
        i=0;
    }
    return i;
}
int main(void)
{
    char Name[10];
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh("NAME,M",Name,2,End);
    //printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}
MrNoob 24 Posting Whiz in Training

and if there is no W it will return 0 anyways i fixed strchr function but i don't think its rlly a cool way to as a fix *

#include <stdio.h>
#define DEBUG
char *Strchr(char *String,char c,int *num) {
    int temp;
    char *ptr;//whish will be ptr to first stuff we found after that char
    while(*String!=0) {
        (*num)++;
        if(*String==c) {
            temp=1;
            ptr=String+1;
            break;
        }
        *String++;
    }
    if(!temp)
        *num=0;
    return ptr;
}
int main(void)
{
    char name[]="Namee";
    int num=0;
    char *ptr;
    ptr=Strchr(name,'m',&num);
    if(ptr=='\0')
        puts("i m sorry we didnt find any shit");
    else
        printf("We found it at character num %d and we returned this string %s\n",num,ptr);
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

yah

MrNoob 24 Posting Whiz in Training

maybe i got an idea like put a variable if it find it set it to 1 and after while loop ends i chk if its 1 or 0 if its 1 i will just return if its 0 then i will set num to 0 but i feel its kinda noobish dont you think ?

MrNoob 24 Posting Whiz in Training

but if it doesnt find it num will be incremented i want only incremented when it finds it

MrNoob 24 Posting Whiz in Training

yah I just noticed it too that it start at 2nd character also but it wont solve the problem since every time the loop runs it will set again *num to 0 coz its not = to the character maybe i should replace the else with an if but it will be same problem too.

MrNoob 24 Posting Whiz in Training

no *string++ it increment the address of the string *++string increment each character inside

MrNoob 24 Posting Whiz in Training

but while(*string++) is same as (*string!=0) since it will alawys return true till it finds 0 in the end which will encounter when string end

MrNoob 24 Posting Whiz in Training

i thought if it doesnt find string it intilise it to 1 or ? does it keep incrementing while loop is running and each character it doesnt find it it keep reseting to 1 thats y it turned to 1 coz it got incremented 1 time ?if so how can i fix this ??

MrNoob 24 Posting Whiz in Training

it just returns 1 not more than that for int *num for first function

MrNoob 24 Posting Whiz in Training

nah it act as strcpy that will copy string according to number u give or when it has seen , newline or others and i did ++(*num); work but it only incremented number one time not according to how many characters it copied

MrNoob 24 Posting Whiz in Training

also my other function is acting weird i dunno what are those problems keep happening here when i incremented the num++ i get weird results but when i leave it i get it fine !!!! thats not even logical here look

#include <stdio.h>
#define DEBUG
char *Strchr(char *String,char c,int *num) {
    char *ptr;//whish will be ptr to first stuff we found after that char
    while(*String++) {
        #ifdef DEBUG//if this is added i get weird results;
        *num++;
        printf("num is now %d",*num);//this is weird too when i try print the number it prints as address not number
        #endif
        if(*String==c) {
            ptr=String;
            break;
        }
        else {
            ptr='\0';
            *num=0;
        }
    }
    return ptr;
}
int main(void)
{
    char name[]="Namee";
    int num=0;
    char *ptr;
    ptr=Strchr(name,'m',&num);
    if(ptr=='\0')
        puts("i m sorry we didnt find any shit");
    else
        printf("We found it at character num %d and we returned this string %s",num,ptr);
    return 0;
}

i dont know how simple *num++ can affect the string anyways when i remove it the function itself doesnt work it returns 0 character
but when i remove it work fine .....

MrNoob 24 Posting Whiz in Training

Hello i m suppose to make a function that fetch the strings according to num and it will stop when num is finished or it encounter , or ' ' or newline i made the function but its acting weird as if there is a \0 placed in

#include <stdio.h>
int ParseCh(char *N,char *Buffer,int n,char *STend) {
    int i,x=0,end=0;
    while(*N++) {
        end++;
        if(*N==',' || *N==' ' || *N=='\n'){
            STend=N+1;
            break;
        }
        else {
            end=0;
            STend='\0';
        }
    }

    if(!end)//if end is 0 then there wasnt any new line etc we just do normal copy
        for(i=n;Buffer[x]=N[i];i++,x++);
    else//if we found end bigger than 0 then we will just copy till end
        for(i=n;Buffer[x]=N[i] && Buffer[x]<end; i++,x++);

    return x;
}
int main(void)
{
    char Name[10];
    char *End;//last string after the , or new lines etc if its initlised to 0 it means there was no , new lines or tab inside the string
    ParseCh("NAME,M",Name,2,End);
    printf("Name after being parsed %s and it ended at %s\n",Name,End);
    return 0;
}
MrNoob 24 Posting Whiz in Training

oh cool thanks mate nice way of putting it to

MrNoob 24 Posting Whiz in Training

oh thanks man i understand
p has same address of first element of name[] but the char *name itself is in diff memory right ?

MrNoob 24 Posting Whiz in Training

but if it does why the hell you cant cancantate stuff in main when u do char *name="bla"; but in a function u can do it ?? thats so weird thanks guys in advance

MrNoob 24 Posting Whiz in Training

i think i knew why coz Name is a char ptr does it get assigned to the address of the Name then it can increment address coz its char ptr ?

MrNoob 24 Posting Whiz in Training

well i alawys wondered when i make char name[]="bla"; and count by ptr it doesnt work it says cant count wrong argument to increment
but see whenever i like make it a function like void putit(char *Name);
it work i dunno why though why does it work then if its an array ? does it cast it or something ? like take for example this code here

#include <stdio.h>
//put it function work but how so even i use normal array in name it wasnt a chr ptr ??
void Put_it(char *Name) {
     while(*Name)
         putchar(*Name++);
}
int main(void)
{
    char name[]="blabla";
    while(*name)
        putchar(*name++);//doesnt work
    return getchar();
}

if it does cast it why when i cast in putchar in main function it doesnt work ??

MrNoob 24 Posting Whiz in Training

a pie char you must use graphics function or maybe like draw one by * but that will get somewhat confusing because u have to make it according to your % stuff

MrNoob 24 Posting Whiz in Training

i think you can do this for the ; thing u do a list with stuff that doesnt have have ;(that is if it has more than 1 line) like for example for while if else if etc then after that if its not in the list chk the last char in line
if its ; or blank if its blank you just replace it with ; i think you can use strchr will help you for this and for other indetation

MrNoob 24 Posting Whiz in Training

just do type casting

MrNoob 24 Posting Whiz in Training

yah i didnt notice that dam me :P thanks case solved :P

MrNoob 24 Posting Whiz in Training

well i fixed it but i dunno why before i had -1

#include <stdio.h>
#include <windows.h>
#define SIZE 6
int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice);
/*CopyElement.c*/
/*it will return num of elements copied and choice
/*if wether or not we want to use user pointers calculating or normal calculation*/

int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice) {
    int elments=0;
    switch(choice) {
        case 0:
        {

            int i;
            for(i=0;i<end2;i++)
                target[i]=Mainarr[i];
            elments=i;
        }
            break;
        case 1:
            while(target<=end) {
                elments++;
                *target++=*Mainarr++;
            }
            break;
    }
    return elments;
}
void Showarr(int array[],int end) {
    int i;
    for(i=0;i<end;i++)
        printf("%d\n",array[i]);
}
int main(void)
{
    int  const Main[]={1,2,3,5,6,7};
    int Copy[SIZE]={0};//will Copy Elements of Main
    int result;
    result=CopyElements(Main,Copy,Copy+SIZE,sizeof Copy/sizeof Copy[0],0);
    printf("alright you copied %d elements\n",result);//says i copied only 6 elments not more
    puts("okay now showing it");
    Showarr(Copy,sizeof Copy/sizeof Copy[0]);
    return 0;
}
MrNoob 24 Posting Whiz in Training

okay thanks mate but what abt -1 being copied to the array ?

MrNoob 24 Posting Whiz in Training

also i made the function here but i get a variable added to the array which is -1 i dunno why its being added

#include <stdio.h>
#define SIZE 5
/*CopyElement.c*/
/*it will return num of elements copied and choice
/*if wether or not we want to use user pointers calculating or normal calculation*/

int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice) {
    int elments=0;
    switch(choice) {
        case 0:
        {
            int i;
            for(i=0;Mainarr[i]<end2;i++)
                target[i]=Mainarr[i];
            elments=i;
        }
            break;
        case 1:
            while(target<=end) {
                elments++;
                *target++=*Mainarr++;
            }
            break;
    }
    return elments;
}
void Showarr(int array[],int end) {
    int i;
    for(i=0;array[i]<=end;i++)
        printf("%d\n",array[i]);
}
int main(void)
{
    int  const Main[]={1,2,3,5,6,7};
    int Copy[SIZE];//will Copy Elements of Main
    int result;
    result=CopyElements(Main,Copy,Copy+SIZE,sizeof Main,1);
    printf("alright you copied %d elements\n",result);//says i copied only 6 elments not more
    puts("okay now showing it");
    Showarr(Copy,sizeof Copy);
    return 0;
}

also one last question i never i understood why when i have in a for loop and i wanna automatic end when it reached the sizeof the array like for example this code

#include <stdio.h>
int main(void)
{
    int arr[]={1,2,3};
    int i;
    //this works
    for(i=0;arr[i]<sizeof arr;i++)
         printf("%d ",arr[i]);
    //this doesnt
    for(i=0;arr[i]<sizeof arr;i++)
         printf("%d ",arr[i]);
    getchar();
    return 0;
}

i dunno why first one works and 2nd one doesnt ?maybe because first 1 which is i ofcoz it doesnt work coz i m compring i to a sizeof which i think has couple hundered so it will keep incrementing i ?
and 2nd one coz i m compring addresses ?
please someone …

MrNoob 24 Posting Whiz in Training

oh but why isnt Mainarr same size as Target why when u made Target<end and yah i forgot to increment target

MrNoob 24 Posting Whiz in Training

hello i m given a question in book i m reading that i copy element but using ptr to copy and other choice will be using normal integers to copy
i did normal integer but copy i think i got a problem with it

#include <stdio.h>
int copyarr(const int Mainarr[],int Target[], int * end);

int copyarr(const int Mainarr[],int Target[], int * end)
{
    while (Mainarr < end)
        *Target=*Mainarr++;
}

int main(void)
{
    int const Mainarr[]={1,2,3};
    int target[2];
    int *end;
    end=target+3;
    copyarr(Mainarr,target,end);
    printf("%d %d %d\n",target[0],target[1],target[2]);//lazy to do a for loop :P
    getchar();
    return 0;
}
MrNoob 24 Posting Whiz in Training

yah sorrry had to go i ill mark it as solved now

MrNoob 24 Posting Whiz in Training

yah thanks mate i understand now for the return value of main question i didnt search because question came up with the thread anyways thanks again guys u alawys clear up stuff i dont understand

MrNoob 24 Posting Whiz in Training

how can i chk if it exited successfully is there a way to chk program return value ?

MrNoob 24 Posting Whiz in Training

Whats with return getchar() ? You got several other ways to pause the output.Use one of them rather than this because even though you return some random character as the return of main and exit the OS thinks that the process didn't terminate properly because of a non zero return value.Which of course doesn't cause any problem here but would definitely cause great problems in future when you write some important code.
So better correct it now.

well for getchar() coz sometimes i use dev when testing a program its a habbit of me to put return getchar(); rather than writing small thing since its a program made for learning

MrNoob 24 Posting Whiz in Training

yah i know now but what if i wanna count like row by row like
{1,3,4} {3,5,6} it will count 1 + 3 and 3+5 and 6 + 4 ?
should i switch variables x first then i 2nd ?

MrNoob 24 Posting Whiz in Training

hey guys i know its kinda noobish not to understand them since i been coding for 1 month but there rlly some stuff which confuses me in multidimensional array and nested loops like here in this program
I wanted to know if i sum up like in the array for example num[5][10] i wanted to know which get summed first if i did total+=num[x]; but here in this program i made i got weird error which i think shouldn't produce it so please someone enlighten me because till now i understand all stuff pointers memory management and hard stuff in C but i don't understand these multi and nested loops

#include <stdio.h>
int main(void)
{
    int num[2][3]= {
    {1,1,1},{1,1,1}
    };
    int i;
    int x;
    int total=0;
    for(i=0;i<2;i++) 
       for(i=0;i<3;i++) {
            total+=num[i][x];
            }
       printf("%d",total);
    return getchar();
}

shouldnt it produce 6 ? please someone enlighten me in understanding this ....

MrNoob 24 Posting Whiz in Training

haha

MrNoob 24 Posting Whiz in Training

oh thanks if only the book would put it in that simple

MrNoob 24 Posting Whiz in Training

oh so it keep calling function till ruc level 4 then it goes to other function whish level 3 then level 2 then level 1 again whish is the main one ?

MrNoob 24 Posting Whiz in Training

Hey i m reading tutrial about rucursion and got code whish isnt complicated or anything but i dunno why it prints the way it does

/* recur.c -- recursion illustration */

#include <stdio.h>

void up_and_down(int);



int main(void)

{

    up_and_down(1);

    return 0;

}



void up_and_down(int n)

{

    printf("Level %d: n location %p\n", n, &n); /* 1 */

    if (n < 4)

         up_and_down(n+1);

    printf("LEVEL %d: n location %p\n", n, &n); /* 2 */



}

here it prints
Level 1: n location 0022FF60
Level 2: n location 0022FF40
Level 3: n location 0022FF20
Level 4: n location 0022FF00
LEVEL 4: n location 0022FF00
LEVEL 3: n location 0022FF20
LEVEL 2: n location 0022FF40
LEVEL 1: n location 0022FF60


first i know why it prints from 1 to 3 but see here it keeps calling itself till number is smaller than 4 right then after it finishes it goes back to that printf after that if statement whish should print the normal i whish is 1 but it doesnt it prints 4 4 3 2 1 why it got decremented there nowhere in the code where it said i-1 even when the function called itself even if we alrdy specified i as 1 and function calling itself somehow decremented it should when i==3 that if statement will terminate whish will print then orginal i ?