0
How to Change arrangement of letters in a word using c programing ? For example changing word Camera to meraca.
10 Respostas
+ 1
Nice Brian, however thatâs not changing the arrangement of letters, nothing changed - just print formatting.
From the updated question it looks like Rohit wants to move the first two characters of a string to the end.
Hereâs an example that takes a string as input, then moves the first two chars to the end.
https://code.sololearn.com/cvjqD4m4mTHS/?ref=app
+ 3
Please explain a little more - how do you want the letters rearranged?
For example: âtriboreâ
Reversed: âerobirtâ
Random : ârbtireoâ
+ 2
You access the desired character of the string by specifying its index. As a string is a character array:
#include <stdio.h>
int main() {
char str[] = "Hello";
char temp = str[0];
str[0] = str[2];
str[2] = temp;
printf("%s", str); //output is "leHlo"
return 0;
}
The letter l and H were swapped as the placeholder variable temp was required to hold the latter character to prevent losing it. Ergo, the arrangement of letters have changed.
Your description is rather vague, so if this answer was not what you were looking for, then be more descriptive.
+ 1
Syt->Show Your Try
also please explain little bit more
https://code.sololearn.com/WGoRmEn9KQz5/?ref=app
+ 1
C Program to sort a string/letter in alphabetical order
#include <stdio.h>
#include <string.h>
int main ()
{
char string[100];
printf("\n\t Enter the string : ");
scanf(â%sâ,string);
char temp;
int i, j;
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
printf(âThe sorted string is : %sâ, string);
return 0;
}
+ 1
Brian, agreed, I just thought that was more illustrative as to what is going on.
Also as far as Iâm aware using sprintf would require a new char array of the same size (more memory overhead) and wouldnât re-arrange in place.
For completeness Iâve added the sprintf method you mentioned and also str copy/concat...feel free to add any additional methods.
https://code.sololearn.com/cG2gSWV9rzO1/?ref=app
0
If your wanting to sort alphabetically, a slightly more efficient version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char word[] = "sgrbqpoenmzydfxlcwakvjuiht";
char wordInput[100];
int compare(const void* a, const void* b) {
return (*(char*)a - *(char*)b);
}
void sortAlpha(char cstr[])
{
qsort(cstr, strlen(cstr), (size_t)sizeof(char), compare);
printf("%s\n\n", cstr);
}
int main()
{
printf("%s\n", "Example - String before alphabetical sort");
printf("%s\n\n", word);
printf("%s\n", "Example - String after alphabetical sort");
sortAlpha(word);
printf("Type a string, followed by RETURN:");
scanf_s("%99s", wordInput, 100);
printf("%s\n", "\nYour Input - Before alphabetical sort");
printf("%s ", wordInput);
printf("%s\n", "\nYour Input - After alphabetical sort");
sortAlpha(wordInput);
return(0);
}
0
#include <stdio.h>
int main() {
char word[] = "camera";
printf("%s%.2s", word+2, word);
return 0;
}
0
DavX I guess I missed seeing the original edition of the question. The requirements are sketchy, and it is unclear whether printed output is sufficient or the OP wants the result stored in memory after the rearrangement. If it is the latter, then sprintf() could do the job almost as simply.