Results 1 to 4 of 4

Thread: Program to check whether two string are equal or not

  1. #1
    Join Date
    Nov 2009
    Posts
    85

    Program to check whether two string are equal or not

    Hi Friends,

    I have to write the program , in which I have to check whether two given string are equal or not. I have tried many techniques but not successful.

    Please help me to code this program to compare two string.

    Your help will greatly appreciated...

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Program to check whether two string are equal or not

    Hi all,

    Please try to run the below program. Below code is running successfully on my machine and gives correct output i.e check the two string:

    #include<stdio.h>
    #include<conio.h>

    int strngcmp(const char *a1, const char *a2)
    {
    unsigned int x = 0, diffrc;
    while(*(a1+i) && *(a2+x))
    {
    diff = (*(a1+x)-*(a2+x));
    if(!diffrc)i++;
    else break;
    }
    return diffrc;
    }
    int main()
    {
    printf("chuma %d ", strngcmp("pqos","pqrst"));
    return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Program to check whether two string are equal or not

    Why don't you try below program code to compare two string?

    Program code:

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

    void stringchk(char s1[], char s2[]);

    int main()
    {

    char strg1[10],strg2[10];

    printf("Enter 1st String:");
    scanf("%s",strg1);

    printf("Enter 2nd String:");
    scanf("%s",strg2);

    stringcmp(strg1,strg2);

    return 0;
    }

    void stringchk(char *p1, char *p2)
    {
    int a,b;

    for(a=0;s1[a]!='\0';a++)
    {
    for(b=0;p2[b]!='\0';b++)
    {
    if(p1[a] == p2[b])
    continue;
    }
    }

    if (a==b)
    {
    printf("String p1:%s and p2:%s are Equal\n",s1,s2);
    }
    else
    printf("String p1:%s and p2:%s are Not Equal\n",s1,s2);

    }


  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Program to check whether two string are equal or not

    Hi friends,

    I suggest you to use nested loop here. Refer the below program, I used "If" loop inside the "For" loop. And at the end I am checking the test condition using "if" loop, which check whether given two string are equal OR not

    #include<conio.h>
    #include<iostream.h>
    #include<stdio.h>

    void main()

    {
    clrscr();
    int n=0;
    char l[10];

    char m[10];
    gets(l);
    gets(b);
    for(int a=0,b=0;b[i]!='\0'||x[j]!='\0';a++,b++)
    {
    if(a[i]!=b[j])
    {
    l++;

    }

    }
    if(n==0)
    cout<<"Given strings are matching";
    else
    cout<<"string strings doesn't matching";

    getch();
    }

Similar Threads

  1. How to check if a bad word is present in a string using PHP
    By mpchekuri in forum Software Development
    Replies: 1
    Last Post: 25-03-2012, 12:07 PM
  2. Replies: 7
    Last Post: 02-07-2011, 10:45 PM
  3. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM
  4. How to check java string not null or blank
    By Capper in forum Software Development
    Replies: 4
    Last Post: 30-07-2009, 07:11 PM
  5. SQL need to check null or empty string
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 18-06-2009, 11:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,020,959.44514 seconds with 16 queries