Please solve these problems

Discussion in 'C' started by bsudhir6, Apr 16, 2010.

  1. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    First one is charecter stuffing

    I got this program executed successfully but One lock within the input string and if I use more than one lock it prints 2 times the next lock situation… please execute and check the error…


    check the following attachment to know how i wrote and i'm attaching the .C files too
    View attachment Charecter Stuffing.pdf


    I'd modified the program which is in above file


    But this time it reprints the second (further) occurrences of LOCK please solve it the .C files are...


    View attachment Charecter Stu1ffing.txt


    And I wrote bit stuffing with same logic please check it… I got errors as

    01111110011111ull pointer assignment Divide error abnormal program termination h..11101001111110


    source code is
    View attachment BitstuffingS.TXT
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if the string is

    LOCKEDbymeLOCKEDbyYOULOCKED

    which one is the correct?
     
  3. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    if it is the given input string then output must be like this

    LOCKLOCKLOCKEDbymeLOCKLOCKEDbyYOULOCKLOCKEDUNLOCK

    but with my program it give like this

    LOCKLOCKLOCKEDbymeLOCKLOCKEDbyYOULOCKLOCKEDbyYOULOCKEDyYOULOCKEDUNLOCK
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if i give you a solution with strtok function it will be a problem?
     
  5. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Sir where i meant to use that function as for my search on google

    char *strtok(char *s1, const char *s2);

    The strtok() function gets the next token from string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok() with a null pointer for the first parameter. The string of delimiters, s2, can differ from call to call.

    what are these tokens in my program and which arrays i must pass to strtok() please reply me soon sir ... I'm getting my exams near future
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    i wanted to say strstr not strtok sorry.


    here is one solution
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define BUFFER 500//maximum letters of the phrase
    
    void getString(char *,int);
    void replaceALL(char *, char *, char *);
    void substring(char *,int,int);
    
    int main(){
        char BEG[]="LOCK",END[]="UNLOCK";//Strings we put at the begining and ending of input string
        char REPLACE[10];
        strcpy(REPLACE,BEG);// we consruct our
        strcat(REPLACE,BEG);// replace string-->"LOCKLOCK"
        char Input[BUFFER];//input phrase
        char Output[BUFFER];//output phrase
        int count=0;
        printf("\nenter phrase:");
        getString(Input,BUFFER);//store our phrase into Input (char array)
        replaceALL(Input,BEG,REPLACE);//replace all instances of BEG string inside our phrase with replace string
        strcpy(Output,BEG);//now you put our first word
        strcat(Output,Input);//our phrase with replaced instanses of BEG
        strcat(Output,END);//and finally we add the word that finishes our phrase
         printf("\n output=%s",Output);//we print it ,in order to check it
        getchar();//pause
        return 0;
    }
    
    
    void getString(char *string,int buffer){//gets phrase into string variable
        int i=0;
        fgets ( string, buffer, stdin );
        for ( i = 0; i < buffer; i++ ){
            if ( string[i] == '\n' ){
                string[i] = '\0';
                break;
            }
        }
    }
    
    void replaceALL(char *str, char *orig, char *rep){
        char *p;
        char temp[500];
        static char buffer[500];
        static char output[500]="";
        strcpy(temp,str);
        if (strstr(temp, orig)==NULL) return;//if we do not find the BEG inside the string we exit function
        while((p = strstr(temp, orig))!=NULL){//if we find BEG inside string then
            strncpy(buffer, temp, p-temp);//we copy in buffer variable the characters before the BEG was found
            buffer[p-temp] = '\0';//we end string
            sprintf(buffer+(p-temp), "%s%s", rep, p+strlen(orig));//we add the changed value with the rest of the string
            int from=temp-p;
                if (from<0) from=-from;
                from+=strlen(rep);//from =from which char index of the array to cut
    
                strcpy(temp,buffer);
           int to=strlen(temp)-1;//to=until which char index to cut
                substring(temp,from,to);//the string after the replacement of BEG
            substring(buffer,0,from-1);//the replaced string
            strcat(output,buffer);//we add it to the result
        }
        strcat(output,temp);//the left overs
        strcpy(str,output);//the result
    }
    void substring(char *string,int from,int to){//cuts a piece of a string from,to 
        int count=0;
        if (from>to || strlen(string)<=to || strlen(string)<=from) return;
        char temp[1000];
        for (int i=from;i<=to;i++){
            temp[count++]=string[i];
        }
        temp[count]='\0';
        strcpy(string,temp);
    }
    

    and second solution based on your code is

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <string.h>
    
    char BEG[]="LOCK",END[]="UNLOCK";//Strings we put at the begining and ending of input string
    
    int main()
    {
    	int i,j,k,p,l;
    	char tmp[100]="",S[100]="",S1[100]="",x[200]="";
    	printf("Enter the input string:");
    	gets(S);// example SDLOCKEDBYDS
    	strcpy(tmp,BEG);
    	l=strlen(S);
    	int count=-1;
    	for(i=0;i<l;i++){
            x[++count]=S[i];
           if ((i+3)<l){
    		if(S[i]=='L' && S[i+1]=='O' && S[i+2]=='C' && S[i+3]=='K'){
                    x[++count]='O';
                    x[++count]='C';
                    x[++count]='K';
                    x[++count]='L';
                    x[++count]='O';
                    x[++count]='C';
                    x[++count]='K';
                    i=i+3;
                }
            } 
            }
            x[++count]='\0';
        strcat(tmp,x);
    	strcat(tmp,END);
    	printf("\nThe String after charecter stuffing is \n\n");
    	puts(tmp);
    	getch();
    }
    
    
     
    Last edited: Apr 18, 2010
  7. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Sir
    What about the bit stuffing problem please help it... too

    and i'm posting a new program which is written for implementing optimal page replacement algorithm in a new post please help with it sir...
     
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    so the program for bits is this
    check it.

    Code:
    
    #include<stdio.h>
    #include<conio.h>
    #include <string.h>
    
    char FLAG[]="01111110";//Strings we put at the begining and ending of input string
    
    int main()
    {
        int i,j,k,p,l;
        char tmp[100]="",S[100]="",S1[100]="",x[200]="";
        printf("Enter the input string:");
        gets(S);// example SDLOCKEDBYDS
        strcpy(tmp,FLAG);
        l=strlen(S);
        int count=-1;
        for(i=0;i<l;i++){
            x[++count]=S[i];//first 1
           if ((i+4)<l){
                if(S[i]=='1'&& S[i+1]=='1'&& S[i+2]=='1' && S[i+3]=='1'&& S[i+4]=='1'){
                    x[++count]='1';
                    x[++count]='1';
                    x[++count]='1';
                    x[++count]='1';
                    x[++count]='0';
                    i=i+4;
                }
            } 
            }
            x[++count]='\0';
        strcat(tmp,x);
        strcat(tmp,FLAG);
        printf("\nThe String after charecter stuffing is \n\n");
        puts(tmp);
        getch();
    }
    
    
     
    Last edited: Apr 19, 2010
  9. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Sir we just insert a 0 after each occurrence of 5 continuous 1's in the input string.just like character stuffing we put 01111110 on the beginning and ending of input string...
     
  10. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    examples are

    input string: 0111111111111110111111111011111010
    output must be
    01111110 01111101111101111011111011110111110010 01111110

    01111110 is flag bit which will be inserted at beginning and ending

    0 is stuffed bit after each occurrence of five continuous one's in the string
     
  11. ameentarras

    ameentarras New Member

    Joined:
    Apr 27, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hhhhhhhhhhey i want dome help in solving these programs plz

    Write a function called CountMultiplesOfThree that takes as parameters two integers, start and
    stop, counts all integers between start and stop (inclusive) that are multiples of 3, and returns
    the number of these integers. For example if start is 7 and stop is 10 then the function will
    return 1 since only 9 is a multiple of 3.
    Do not write a main for this exercise.
    Hint: use one of the three repetition structures (loop)
     
  12. ameentarras

    ameentarras New Member

    Joined:
    Apr 27, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Write a function with two parameters, the first of type char and the second of type int. The
    purpose of the function will be to print out the first parameter the specified number of times (the
    second parameter is the number of times). Write a main that calls this function to print a square
    formed of any character
     
  13. ameentarras

    ameentarras New Member

    Joined:
    Apr 27, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Write a function isEven that takes a positive number as an attribute, the function checks
    if the number is even. The function returns 1 if the number is even and returns -1
    otherwise. Here is the prototype of the function:
    int is even (int x);

    Write a C program that prompts the user to enter a positive number; The program should
    call the function named isEven (defined in part i) to check if the number entered is Even
    or Odd. After calling the function, a message should be displayed to inform the user that
    the number entered is even or odd.
     
  14. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    hint
    if (A%2==0) .....
    else ......
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Don't hijack other people's threads please. Start your own; it's free. And please start a new one per problem. But don't post all your problems at once, when you've learnt new skills by solving one you may be able to solve the rest without any help.

    Also you should show what you've done so far. Where are you stuck? What code have you written? You need to show some effort because we are not a free homework service.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice