0

How to Change arrangement of letters in a word using c programing ? For example changing word Camera to meraca.

30th Aug 2021, 10:49 AM
Rohit Ranaware
9 Réponses
+ 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
31st Aug 2021, 4:50 PM
DavX
DavX - avatar
+ 3
Please explain a little more - how do you want the letters rearranged? For example: “tribore” Reversed: “erobirt” Random : “rbtireo”
30th Aug 2021, 10:51 AM
DavX
DavX - avatar
+ 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.
30th Aug 2021, 12:21 PM
Magnus
Magnus - avatar
+ 1
Syt->Show Your Try also please explain little bit more https://code.sololearn.com/WGoRmEn9KQz5/?ref=app
30th Aug 2021, 10:50 AM
Vtec Fan
Vtec Fan - avatar
+ 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; }
30th Aug 2021, 12:37 PM
Arun Jamson
Arun Jamson - avatar
+ 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
1st Sep 2021, 11:12 AM
DavX
DavX - avatar
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); }
30th Aug 2021, 4:52 PM
DavX
DavX - avatar
0
#include <stdio.h> int main() { char word[] = "camera"; printf("%s%.2s", word+2, word); return 0; }
31st Aug 2021, 12:09 PM
Brian
Brian - avatar
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.
31st Aug 2021, 5:30 PM
Brian
Brian - avatar