Expected b4 arAnagram


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expected b4 arAnagram
# 1  
Old 11-05-2013
Expected b4 arAnagram

here is the code can't figure to make it work

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


void quickSort(char *arr, int si, int ei);

bool  areAnagram(char *str1, char *str2)
{

int n1 = strlen(str1);
int n2 = strlen(str2);

if (n1 != n2)
return false;

quickSort (str1, 0, n1-1);
quickSort (str2, 0, n2 -1);

for(int i=0; i <n1; i++)
if (str1[i] != str2[i])
return false;

return true;
}
void exhange(char *a, char*b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition(char A[], int si, int ei)
{
char x = A[ei];
int i = (si-1);
int j;
for(j=si; j<= ei-1; j++)
{
if(A[j] <= x)
{
i++;
exchange(&A[i+1], &A[ei]);
}
}
exchange (&A[i+1], &A[ei]);
return (i+1);
}
void quickSort(char A[], int si, int ei)
{
int pi;
if(si<ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi-1);
quickSort(A, pi+1, ei);
}
}
int main()
{
char str1;
char str2;
printf("Enter the first string");
scanf("%d\n", str1);
printf("Enter the second string");
scanf("%d\n", str2);
if (areAnagram(str1, str2))
printf("The two strings are anagram of each other");
else
printf("The two strings are not anagram of each other");
return 0;

---------- Post updated at 09:07 PM ---------- Previous update was at 08:02 PM ----------

this code suppose the compare two strings when you input but there is an error and i can't find a way to solve.
# 2  
Old 11-05-2013
Try this

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


void quickSort(char *arr, int si, int ei);

bool  areAnagram(char *str1, char *str2)
{

int n1 = strlen(str1);
int n2 = strlen(str2);

if (n1 != n2)
return false;

quickSort (str1, 0, n1-1);
quickSort (str2, 0, n2 -1);

for(int i=0; i <n1; i++)
if (str1[i] != str2[i])
return false;

return true;
}
void exchange(char *a, char*b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition(char A[], int si, int ei)
{
char x = A[ei];
int i = (si-1);
int j;
for(j=si; j<= ei-1; j++)
{
if(A[j] <= x)
{
i++;
exchange(&A[i+1], &A[ei]);
}
}
exchange (&A[i+1], &A[ei]);
return (i+1);
}
void quickSort(char A[], int si, int ei)
{
int pi;
if(si<ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi-1);
quickSort(A, pi+1, ei);
}
}
int main()
{
char str1[255];
char str2[255];
printf("Enter the first string: ");
scanf("%s", &str1);

printf("Enter the second string: ");
scanf("%s", &str2);
if (areAnagram(str1, str2))
printf("The two strings are anagram of each other\n");
else
printf("The two strings are not anagram of each other\n");
return 0;
}


Eg:

Code:
$ g++ -o b4 b4.cpp
$ ./b4
Enter the first string: Test
Enter the second string: tseT
The two strings are not anagram of each other

---------- Post updated at 12:55 PM ---------- Previous update was at 12:48 PM ----------

Note this function corrupts the input strings and will most likley crash with a memory violation if you passed constants into it eg:

Code:
if (areAnagram(str1, "unix"))

Best practice would be to manipulate copies of the strings and leave the input params unchanged.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Not getting O/P in expected way-java

through below code i am trying to write a content in a file and then reading the same file my code is running file but is not getting in proper way import java.io.*; class A1 { public static void main (String agrs) { byte b = {'a','e','i','o','u'}; String s = "King Maker"; try {... (7 Replies)
Discussion started by: scriptor
7 Replies

2. Programming

Java not getting in expected way

Hi i am new to java and written a below code on linux platform which is working fine but not getting the output on terminal as expected vi A.java import java.io.FileOutputStream; class A { public static void main (String args) { byte c= {'a','e','i','o','u'}; try {... (1 Reply)
Discussion started by: scriptor
1 Replies

3. Shell Programming and Scripting

Not getting expected result

Hi Experts, I have written the below script but its not working as per expectation. My requirement is if this condition ] is satisfied then only check for this condition ] if this also satisfied check for the condition ]. vi p_values.ksh path="/db/ora/files" mode=1 b_days=10... (5 Replies)
Discussion started by: nalu
5 Replies

4. Shell Programming and Scripting

ERROR: `(' is not expected.

Hi All, I have written a shell script which works all right on bash shell, but when it comes to execute it using ksh on AIX it gives the following error::( bash$ /bin/ksh getShortInfo.sh getShortInfo.sh: syntax error at line 26 : `(' unexpected Could you please indicate what is... (4 Replies)
Discussion started by: Elvis
4 Replies

5. Shell Programming and Scripting

Not getting expected output

Hi I have written below script to get the data in table form. #!/bin/sh echo "File Name\tType" for i in *; do echo "$i\t\c" if ; then echo "directory" elif ; then echo "symbolic link" elif ; then echo "file" else echo "unknown" fi donehowever i am getting output in different way... (3 Replies)
Discussion started by: scriptor
3 Replies

6. Programming

Please Help ! ----> error: expected =,

#include<stdio.h> int main{ char *fl; fl=(char*)malloc(150); strcat(fl,"/tmp/OV/"); printf("\nInside fl--->%s\n",fl); return 0; } I wrote a simple program as above. I got the error error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token Please help me out ! I am... (4 Replies)
Discussion started by: gameboy87
4 Replies

7. Shell Programming and Scripting

elif if not expected

Hi All, I write an script will have some functions... I am getting elif not expected error.. Even tried by using set -x for debug but no use.. Could you please help me out in this Variables .... .... .... TEST_SRC() { cat ${filesrc} >> ${filetgt} if ]; then echo... (12 Replies)
Discussion started by: kmsekhar
12 Replies

8. Shell Programming and Scripting

+: more tokens expected

Hey everyone, i needed some help with this one. We move into a new file system (which should be the same as the previous one, other than the name directory has changed) and the script worked fine in the old file system and not the new. I'm trying to add the results from one with another but i'm... (4 Replies)
Discussion started by: senormarquez
4 Replies

9. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

10. Shell Programming and Scripting

: + : more tokens expected

Hello- Trying to add two numbers in a ksh shell scripts and i get this error every time I execute stat1_ex.ksh: + : more tokens expected stat1=`cat .stat1a.tmp | cut -f2 -d" "` stat2=`cat .stat2a.tmp | cut -f2 -d" "` j=$(($stat1 + $stat2)) # < Here a the like the errors out echo $j... (3 Replies)
Discussion started by: Nomaad
3 Replies
Login or Register to Ask a Question

Featured Tech Videos