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; }

16th Dec 2018, 12:51 PM
Mohib Ulla Khan
Mohib Ulla Khan - avatar
3 Antworten
+ 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.
16th Dec 2018, 1:02 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
Swap function
16th Dec 2018, 12:56 PM
Zeshan Ahmed
Zeshan Ahmed - avatar
+ 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; }
16th Dec 2018, 12:58 PM
Zeshan Ahmed
Zeshan Ahmed - avatar