Results 1 to 4 of 4

Thread: C Coding Question

  1. #1
    Expert Member
    Join Date
    Sep 2005
    Answers
    206

    C Coding Question

    Write a program to get strings and to substitute character from string1 to string2 (equv to UNIX command tr)

    for example:

    string1: "abc"
    string2: "def"

    the occurrence of 'a' will be replaced by 'd' , 'b' will be replaced by 'e' , 'c' will be replaced by 'f' AND for less complexity assume the string1 and string2 are of same size.


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    6

    Re: C Coding Question

    Quote Originally Posted by suji View Post
    Write a program to get strings and to substitute character from string1 to string2 (equv to UNIX command tr)

    for example:

    string1: "abc"
    string2: "def"

    the occurrence of 'a' will be replaced by 'd' , 'b' will be replaced by 'e' , 'c' will be replaced by 'f' AND for less complexity assume the string1 and string2 are of same size.
    #include<stdio.h>
    void main()
    {
    int len1,len2,i;
    char str1[20],str2[20];

    printf("Enter String 1::");
    fflush(stdin);
    scanf("%s",str1);

    printf("Enter String 2::");
    fflush(stdin);
    scanf("%s",str2);

    for(i=0,str1[i]!='\0',i++);
    len1=i;

    for(i=0,str2[i]!='\0',i++);
    len2=i;

    if(len1!=len2)
    {
    printf("Length of both the strings are not same");
    }
    else
    {
    for(i=0,str[i]!='\0',i++)


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    6

    Re: C Coding Question

    Quote Originally Posted by suji View Post
    Write a program to get strings and to substitute character from string1 to string2 (equv to UNIX command tr)

    for example:

    string1: "abc"
    string2: "def"

    the occurrence of 'a' will be replaced by 'd' , 'b' will be replaced by 'e' , 'c' will be replaced by 'f' AND for less complexity assume the string1 and string2 are of same size.


    Sorry, Last post was made wrongly.

    /*Without using library function*/

    #include<stdio.h>
    void main()
    {
    int len1,len2,i;
    char str1[20],str2[20];

    /*getting String 1*/
    printf("Enter String 1::");
    fflush(stdin);
    scanf("%s",str1);

    /*getting String 2*/
    printf("Enter String 2::");
    fflush(stdin);
    scanf("%s",str2);

    /*getting length of string 1*/
    for(i=0;str1[i]!='\0';i++);
    len1=i;

    /*getting length of string 2*/
    for(i=0;str2[i]!='\0';i++);
    len2=i;

    /*Checking length of both the string*/
    if(len1!=len2)
    {
    printf("Length of both the strings are not same");
    }
    else
    {

    /*Replacing characters one by one from String 1 to string 2*/
    for(i=0;str1[i]!='\0';i++)
    {
    str2[i]=str1[i];
    }

    /*Adding '\0' to end of the replaced characters to make it a string*/
    str2[i]='\0';
    printf("After replacement String 1 is %s and String 2 is %s",str1,str2);
    }
    }

    /*Using library function*/

    #include<stdio.h>
    #include<string.h>
    void main()
    {
    int len1,len2,i;
    char str1[20],str2[20];

    /*getting String 1*/
    printf("Enter String 1::");
    fflush(stdin);
    scanf("%s",str1);

    /*getting String 2*/
    printf("Enter String 2::");
    fflush(stdin);
    scanf("%s",str2);
    strcpy(str2,str1);
    printf("After replacement String 1 is %s and String 2 is %s",str1,str2);
    }


  4. #4
    Junior Member
    Join Date
    Dec 2007
    Answers
    12

    Re: C Coding Question

    # include<stdio.h>
    # include<conio.h>
    void main()
    {
    char str[20],str1[20],str2[20],finalStr[20];
    int i=0,l=0,l1=0,l2=0;
    //accepting inputs
    printf("Enter string:");
    scanf("%s",str);
    printf("Enter set of characters to be replace:");
    scanf("%s",str1);
    printf("Enter set of character as replacement:");
    scanf("%s",str2);

    //copying original string into final string
    for (i=0;str[i]!='\0';i++)
    finalStr[i]=str[i];
    finalStr[i]='\0';

    //checking inputs......
    printf("%s\n%s\n%s\n%s",str,str1,str2,finalStr);

    //calculating lengths
    for (l=0;str[l]!='\0';l++); //length of original string
    for (l1=0;str[l1]!='\0';l1++); //length of set of characters to be replace
    for (l2=0;str[l2]!='\0';l2++); //length of set of characters as replacement

    //logic to replace the characters
    if (l1!=l2)
    printf("Length not equal.");
    else
    {
    for (i=0;i<l1;i++)
    {
    for(j=0;j<l;j++)
    {
    if(str1[i]==str[j])
    {
    finalStr[j]=str2[i];
    }
    }
    }
    }
    //printing new string
    printf("Final string : %s",finalStr);
    getch();
    }


    u might 've got d answer but try this out.........


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact