How do I sort lastname by alphabetical order? - c

This program is only for sorting the firstname alphabetically.
My Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char name[10][8], temp[8];
int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("Input name changes alphabetically\n");
for (i = 0; i < n; i++)
{
printf("\t%s\n", name[i]);
}
return 0;
}
(output of the program)
Suppose,Input
Enter the value of 2
merge
bubble
output
Input name changes alphabetically
bubble
merge
But I want to write this program to sort the lastname alphabetically.
suppose,for example
Input
Marop hossain
Nihan ahmed
output
Nihan ahmed
Marop hossain
I'm new to programming so i don’t understand. How do I change the code above to get this result.

the code below should work fine
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char name[10][8], temp[8];
int i, j, n,L,k;
printf("Enter the value of n \n");
scanf("%d", &n);
fflush(stdin);
for (i = 0; i < n; i++)
{
gets(name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
k=0;
while(1) //searches for the whitespace in the string
{
++k;
if(name[i][k]==' ')
break;
}
for (j = i + 1; j < n; j++)
{
L=0;
while(1)
{
++L;
if(name[j][L]==' ')
break;
}
if(name[i][k+1]>name[j][L+1]) //compares the char after the whitespace
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("Input name changes alphabetically\n");
for (i = 0; i < n; i++)
{
puts(name[i]);
}
return 0;
} ```

Related

C program to insert elements into an array until user inputs a 0 or less number

I'm trying to make a C program to insert elements into an array until user inputs a 0 or less number, as the title says. But when I print the array out, it doesn't show the numbers I inputted. I have tried using a while as well as do-while loops but without success.
#include <stdio.h>
int main() {
int data[100];
int i;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
int n = sizeof(data[i]);
for (int i = 0; i < n; i++) {
printf("%d ", &data[i]);
}
}
Try this:
#include <stdio.h>
int main() {
int data[100];
int i;
int counter = 0;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
counter++;
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
for (int j = 0; j < counter - 1; j++) {
printf("%d ", data[j]);
}
}
The problem was that you had printf("%d ", &data[i]); instead of printf("%d ", data[i]);.
And also you've trying to get the sizeof() of an element data[i], not the size of the whole array. That's why there's counter in my code.
int n = sizeof(data[i]);
this is wrong, you want
int n = i;
sizeof(data[i]) gives you the size of an int (4 on my machine)
On the other hand, you need to check the result of scanf, if a bad input is entered do not increment the counter, something like:
int i = 0;
while (i < 100)
{
int res = scanf("%d", &data[i]);
if (res == EOF)
{
break;
}
if (res == 1)
{
if (data[i] <= 0)
{
break;
}
i++;
}
else
{
// Sanitize stdin
int c;
while ((c = getchar()) != '\n');
}
}
Finally, scanf wants a pointer to the object, but this is not the case of printf:
printf("%d ", &data[i])
should be
printf("%d ", data[i])

How can the user input the inputted words in a matrix? #C

This is a code where the user has a 'Word Search Game', the user is has a menu where he can choose to do a new puzzle and then show it and try to work it out. The user needs to enter 4 words and those inputted words need to be shown in a matrix for other users to find and play. My problem with my code is when the user has to enter 4 words, the system crashes and exists the program.
Also how can i put those words which are written by the user inside the matrix?
Code below:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
srand((unsigned)time(NULL));
char Matrix[10][10];
char Location[4][4];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Matrix[i][j] = (rand() % (91 - 65)) + 65;
}
}
int menuChoice;
do
{
system("cls");
puts("WORD SEARCH GAME PUZZLE");
puts("-----------------------");
puts("1. New Puzzle");
puts("2. Show Puzzle");
puts("3. Exit");
puts("-----------------------");
puts("Select your choice: ");
scanf_s("%d", &menuChoice);
switch (menuChoice)
{
case 1: newPuzzle(); break;
case 2: showPuzzle(Matrix); break;
}
} while (menuChoice != 3);
return 0;
}
int newPuzzle()
{
int i;
char word[7];
system("cls");
printf("Enter 4 words of your choice to be entered in the puzzle: \n");
for (i = 0; i < 4; i++)
{
printf("Enter word %d: ", i + 1);
scanf_s("%s", &word);
}
return 0;
}
int showPuzzle(char m[10][10])
{
system("cls");
printf(" ");
char c;
for (c = 'A'; c <= 'J'; ++c)
{
printf("%c ", c);
}
printf("\n\n");
for (int i = 0; i < 10; i++) {
printf("%d ", i);
for (int j = 0; j < 10; j++) {
printf("%c ", m[i][j]);
}
printf("\n");
}
getch();
return 0;
}

Fgets and sscanf not waiting for input C

Originally I was using scanf, but I was running into the newline char getting stuck in the stdin. Everything I have read was saying to switch to fgets and use sscanf instead. With that, I decided to switch to that...but that still is not working. Below you will find my code. My question is, what am I doing wrong with my fgets and sscanf that is causing it to not wait for the user input?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct f_in{
char outline;
int lines;
int rows;
int num_args;
} F_IN;
typedef struct args_in {
char in_string[20];
int t_format;
} ARGS_IN;
void printInterface(char argQs[5][50], char argChar);
int main(int argv, char** argc){
char defaultQuestions[5][50] = { { "1) What char for border?" }
, { "2) Add question" }
, { "3) Remove Question" }
, { "4) Print last answers" }
, { "5) Exit" } };
int commandEntry, exitFlag;
char borderChar = '*', addQ[50],userInp[1];
exitFlag = 1;
while (exitFlag){
printInterface(defaultQuestions, borderChar);
printf("Enter the integer value for the command you wish to select: ");
fgets(userInp, sizeof(userInp),stdin);
sscanf(userInp,"%d", &commandEntry);
printf("\nYou selected: %s\n", defaultQuestions[commandEntry - 1]);
userInp[0] = 0;
if (commandEntry == 1){
printf("Please enter the character you wish to be the border: ");
fgets(userInp,sizeof(userInp),stdin);
sscanf(userInp,"%c",&borderChar);
}
else if (commandEntry == 2){
printf("What question would you like to add? (only enter 50 char max)\n");
fgets(addQ, 50, stdin);
printf("This was your question: %s", addQ);
}
else if (commandEntry == 5){
printf("Goodbye!\n");
exitFlag = 0;
}
}
return 0;
}
void printInterface(char argQs[5][50], char argChar){
int i, j;
int lineCnt = 13;
int borderLen = 75;
for (i = 0; i<100; i++){
printf("\n");
}
for (i = 0; i<lineCnt; i++){
if (i == 0 || i == lineCnt - 1){
for (j = 0; j<borderLen; j++){
printf("%c", argChar);
}
printf("\n");
}
else if (i >= 3 && i <= 10){
printf("%c %s", argChar, argQs[i - 3]);
for (j = 0; j < ((borderLen - strlen(argQs[i - 3]))-6); j++){
printf(" ");
}
printf("%c\n", argChar);
}
else{
for (j = 0; j<borderLen; j++){
if (j == 0){
printf("%c", argChar);
}
else if (j == (borderLen - 1)){
printf("%c\n", argChar);
}
else{
for (j = 0; j<borderLen; j++){
if (j == 0){
printf("%c", argChar);
}
else if (j == (borderLen - 1)){
printf("%c\n", argChar);
}
else{
printf(" ");
}
}
}
}
for (i = 0; i<10; i++){
printf("\n");
}
}
"userInp[1] only allows enough memory to store the terminating '\0'"
- user312023

C: Alphabetical sorting of array of character arrays

This showed up in our laboratory finals examination:
Make a program that takes in 10 string inputs into an array.
Then outputs the strings in alphabetical order.
I couldn't figure it out during the examination and now I want to know how exactly is it done.
So far this is what I've done. It doesn't work well with similar or equivalent strings, their index gets lost? Anyone can share their solution using only the stdio.h and string.h libraries?
/*Write a program that takes 10 strings input into an array and outputs them in alphabetical order*/
#include<stdio.h>
#include<string.h>
char strings[10][150];
char ordered[10][150];
int i,j,k;
int ind;
main()
{
printf("INPUT 10 STRINGS\n");
for(i=0;i<10;i++)
{
gets(strings[i]);
}
for(i=0;i<10;i++)
{
ind=0;
for(j=0;j<10;j++)
{
if(strings[i][0]<strings[j][0])
{
ind++;
}
else if(strings[i][0]==strings[j][0])
{
k=0;
while((strings[i][k]==strings[j][k])&&strings[j][k+1]!='\0')
{
if(strlen(strings[i])<strlen(strings[j]))
{
if(strings[i][k+1]=='\0')
{
ind++;
}
else if(strings[i][k+1]<strings[j][k+1])
{
ind++;
}
}
else if(strlen(strings[i])>strlen(strings[j]))
{
if(strings[i][k+1]<strings[j][k+1])
{
ind++;
}
}
k++;
}
}
}
strcpy(ordered[ind],strings[i]);
}
printf("STRINGS: \n");
for(i=9;i>-1;i--)
{
puts(ordered[i]);
}
}
Just Found a simple way for that:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
gets(str[i]);
}
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
{
puts(str[i]);
}
return 0;
}
strcmp and strcpy are inbuilt functions defined in string.h
for (i=0; i<10; i++) {
for (j=0; j<9; j++) {
if (strcmp(strings[j], strings[j+1]) > 0) {
strcpy(temp, strings[j]);
strcpy(strings[j], strings[j+1]);
strcpy(strings[j+1], temp);
}
}
}
Here's a solution retrieved from here that does what you want but with 5 strings instead. I have adapted it so that it sorts 10 strings instead of 5. All strings have 20 characters at most :
#include<stdio.h>
#include<string.h>
void main() {
char s[10][20], t[20];
int i, j;
clrscr();
printf("\nEnter any five strings : ");
for (i = 0; i < 10; i++)
scanf("%s", s[i]);
for (i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
if (strcmp(s[j - 1], s[j]) > 0) {
strcpy(t, s[j - 1]);
strcpy(s[j - 1], s[j]);
strcpy(s[j], t);
}
}
}
printf("\nStrings in order are : ");
for (i = 0; i < 10; i++)
printf("\n%s", s[i]);
getch();
}

Character patterns in C

I'm trying to get the following character pattern as follows by only using "for" or "while" loops but no "if" or "if-else" statements: (Mentioned in code block)
It's difficult for me to figure out a way to make the first character appear as blank space or represent it even as a null character. Below is my code:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch[6], j='\0';
int p,q,n,i;
printf("Enter a character\n");
for (n = 0; n < 5; n++)
{
scanf_s(" %c", &ch[n], 1);
}
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);// _-->A blank space
} // BA
printf("\n"); // CBA
} // DCBA
// EDCBA
_getch();
return 0;
}
I cannot figure out where I'm going wrong — can you help?
I changed this
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);
}
printf("\n");
}
for this
printf("\n_\n");
for (i = 1; i < 5; i++) {
for (p = i; p >= 0; p--) {
printf("%c", ch[p]);
}
printf("\n");
}
And got this output (console):
Enter a character
ABCDE
_
BA
CBA
DCBA
EDCBA
#include <stdio.h>
#include <conio.h>
#define SIZE 5
int main(void){
char ch[SIZE+1] = {0};
int i;
printf("Enter a character\n");
for (i = SIZE-1; i >= 0; --i){
scanf_s(" %c", &ch[i], 1);
}
printf(" \n");
for (i = SIZE-2; i >= 0; --i) {
printf("%s\n", ch + i);
}
_getch();
return 0;
}

Resources