Please solve these problems

bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
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
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...


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
BitstuffingS.TXT
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
if the string is

LOCKEDbymeLOCKEDbyYOULOCKED

which one is the correct?
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
Quote:
Originally Posted by virxen View Post
if the string is

LOCKEDbymeLOCKEDbyYOULOCKED

which one is the correct?
if it is the given input string then output must be like this

LOCKLOCKLOCKEDbymeLOCKLOCKEDbyYOULOCKLOCKEDUNLOCK

but with my program it give like this

LOCKLOCKLOCKEDbymeLOCKLOCKEDbyYOULOCKLOCKEDbyYOULO CKEDyYOULOCKEDUNLOCK
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
if i give you a solution with strtok function it will be a problem?
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
Quote:
Originally Posted by virxen View Post
if i give you a solution with strtok function it will be a problem?
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
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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 by virxen; 18Apr2010 at 18:44..
bsudhir6 like this
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
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...
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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 by virxen; 19Apr2010 at 06:05..
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
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...
0
bsudhir6's Avatar, Join Date: Apr 2010
Go4Expert Member
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