0
im getting incompatible error for this code char[] cant be converted to string soution?
public static String sort(String s1) { char[] ch=s1.toCharArray(); for(int i=0;i<ch.length;i++) { for(int j=i+1;j<ch.length;i++) { if(ch[i]>ch[j]) { char temp=ch[i]; ch[i]=ch[j]; ch[j]=temp; } } } return ch; }
3 Answers
+ 9
You're trying to return ch which is a character array. But the return type of your method is String. You can change your return type into char[], or convert ch into String and return it.
+ 1
Swap function
+ 1
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,temp;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
temp=Arr[0];
Arr[0]=Arr[n-1];
Arr[n-1]=temp;
cout<<"\nArray after swapping"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}