Enter text and sort alphabetically - c

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[100];
char str2[100];
char str3[100];
int count, tmp;
int i, j=0, k=0;
scanf("%s", str1);
count=strlen(str1);
for(i=0; i<count; i++) {
if(islower(str1[i])) {
str2[j]=str1[i];
j++;
}
else if(isupper(str1[i])) {
str3[k]=str1[i];
k++;
}
}
str2[j]='\0';
str3[k]='\0';
for(i=0; i<strlen(str2); i++) {
for(j=0; j<i; j++) {
if(str2[j]>str2[j+1]) {
tmp=str2[j];
str2[j]=str2[j+1];
str2[j+1]=tmp;
}
}
}
for(i=0; i<strlen(str3); i++) {
for(j=0; j<i; j++) {
if(str3[j]>str3[j+1]) {
tmp=str3[j];
str3[j]=str3[j+1];
str3[j+1]=tmp;
}
}
}
printf("lowercase alignment : %s\n", str2);
printf("uppercase alignment : %s", str3);
return 0;
}
I want to get a string input, divide it into uppercase and lowercase letters, and print it out in alphabetical order. However, errors continue to occur during the sorting process. Why did you do that?

I believe the problem is here:
for (i = 0; i < strlen(str2); i++) {
for (j = 0; j < i; j++) {
For this type of sort, I believe this should be:
for (i = 0; i < strlen(str2); i++) {
for (j = 0; j < strlen(str2) - 1; j++) {
That is, the outer loop needs to be done strlen(str2) times, but its i iteration variable should have no effect on the inner loop which needs to walk (nearly) the entire string on each iteration. Thus the inefficiency of this sort method. Ditto the same looping construct that sorts str3.
My rework of your code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char mixedcase[100];
char lowercase[100];
char uppercase[100];
scanf("%s", mixedcase);
size_t length = strlen(mixedcase);
int l = 0, u = 0;
for (int i = 0; i < length; i++) {
char c = mixedcase[i];
if (isalpha(c)) {
if (islower(c)) {
lowercase[l++] = mixedcase[i];
} else {
uppercase[u++] = mixedcase[i];
}
}
}
lowercase[l] = '\0';
uppercase[u] = '\0';
length = strlen(lowercase);
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - 1; j++) {
if (lowercase[j] > lowercase[j+1]) {
char tmp = lowercase[j];
lowercase[j] = lowercase[j+1];
lowercase[j+1] = tmp;
}
}
}
length = strlen(uppercase);
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - 1; j++) {
if (uppercase[j] > uppercase[j+1]) {
char tmp = uppercase[j];
uppercase[j] = uppercase[j+1];
uppercase[j+1] = tmp;
}
}
}
printf("Lowercase alignment: %s\n", lowercase);
printf("Uppercase alignment: %s\n", uppercase);
return 0;
}
The next thing to do would be to move your sort logic into its own function and call that function on each of the strings to be sorted rather than repeat the code twice in the body of main().

Related

How to make 2D array from 1D array of char string in C

This is code, I've got after making similar function where string are integers, but something is wrong when I change int with char
int main()
{
char A[ ] = "RRTTYYHH";
int len = 8;
char B[2][4];
int i;
int j;
int k = 0;
int row = 2;
int column = 4;
for (i = 0; A[i] < row; i++)
{
printf("\n");
if(k == len)
break;
for (j = 0; A[j] < column; j++)
{
B[i][j] = A[k];
printf("%s\t", B[i][j]);
k++;
}
}
return 0;
}
In the for loops, change the A[i] to i and A[j] to j to iterate correctly over rows and columns.
In the printf, you can use %c instead of %s, as the chars in the 2D version won't be null terminated (like strings or %s expect).
Example:
int main()
{
char A[] = "RRTTYYHH";
int rows = 2, columns = 4;
char B[rows][columns];
int k = 0;
for(int i = 0; i < rows; i++)
{
printf("\n");
for(int j = 0; j < columns; j++)
{
B[i][j] = A[k];
printf("%c\t", B[i][j]);
k++;
}
}
return 0;
}
In printf you can use %c instead of %s
int main()
{
char A[] = "RRTTYYHH";
int len = 8;
char B[2][4];
int k = 0,i=0,j=0;
int rows = 2,cols = 4;
while(i<rows)
{printf("\n");
j=0;
while(j<cols)
{
B[i][j] = A[k];
printf("%c\t", B[i][j]);
j++;
k++;
}
i++;
}
return 0;
}

How to print a 2D array with dots inside them in C

how would I input a 2D array in C, filled with dots?
Here is the code I have written so far, but I still don't see an output of a 2D array with dots.
#include <stdio.h>
#include <stdlib.h>
#define PLAYER_NONE 0
#define PLAYER 1
#define PLAYER_CPU 2
int i; // Global Variable for column and row
int j; // Global Variable for column and row
char playerBoard[8][8]; //Global Variable
char cpuBoard[8][8]; // Global Variable
//Initialise the main parts of the board
void initialise_board(void)
{
for (int i = 0; i < 8; i++) { //iterate the rows
for (int j = 0; j < 8; j++) {
cpuBoard[i][j] = '.';
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
playerBoard[i][j] = '.';
}
}
}
int main(int argc, char *argv[])
{
initialise_board();
while (!check_win()) {
display_board();
get_move(turn);
turn = (turn == 1) ? 2 : 1;
return 0;
}
//Display battleship board when a turn is played
void display_board()
{
printf("\n");
for (char i = 'A'; i < 'H' + 1; i++) {
printf("%c", i);
}
printf("\n");
for (i = 1; i < 8 + 1; i++) {
printf("%d",i);
for (j = 0; j < 8; j++) {
}
printf("\n");
}
printf("===");
printf("\n");
}
Any help would be much appreciated, Thank you very much
Sometimes, while troubleshooting a seemingly impossible problem, it is a good idea to remove all distractions from the code:
Run a simplified main() function:
int main(int argc, char *argv[])
{
initialise_board();
//while (!check_win()) {
display_board();
// get_move(turn);
// turn = (turn == 1) ? 2 : 1;
return 0;
}
And you will see some output, which you can then debug to adjust as needed.
However, As you point out in your comments, you are not seeing output. If you look closely, you will discover that is because you are never including cpuBoard or playerBoard in a printf statement, such as:
printf("%c", cpuBoard[i][j]);
The following will not finish this for you, but will get you started:
void display_board(void)
{
//printf("\n");//removed for illustration
//for (char i = 'A'; i < 'H' + 1; i++) {
// printf("%c", i);
//}
printf("\n");
for (i = 1; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
printf("%c", cpuBoard[i][j]); //illustrates printout of populated array cpuboard.
}
printf("\n");
}
printf("===");
printf("\n");
}
Your code looks fine. Check for the opening and closing brackets and add following statement in initializeboard() function :
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
printf("%c",playerBoard[i][j]);
}
}

printing a matrix adress instead of values | C

I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}

Array[0] is changing while entering the "for" loop, can't figure out why

I'm doing a challenge on HackerRank got the method figured out but there's a slight error I cannot figure out. Further information if needed is https://www.hackerrank.com/challenges/sparse-arrays
Basically I only have a problem with arr[0]. It's storing arr[0] as 'aba', then once it hits the first for loop it changes to 'ab'. Why?
Input:
4
aba
baba
aba
xzxb
3
aba
xzxb
ab
Code:
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
char* test[q];
char* s;
int counter[q];
for (i = 0; i < q; i++) {
counter[i] = 0;
}
for (i = 0; i < n; i++) {
arr[i] = malloc(20);
scanf("%s", arr[i]);
}
scanf("%d", &q);
for (i = 0; i < q; i++) {
test[i] = malloc(20);
scanf("%s", test[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < q; j++) {
if (strcmp(arr[i], test[j]) == 0) {
counter[j]++;
} else {
}
}
}
for (i = 0; i < q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
You declared test and counter as array of size q before having initialize q. Move there declaration just after scanf("%d",&q);. Also move the initializing loop of counter :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
char* s;
for(i=0; i<n; i++) {
arr[i]= malloc(20);
scanf("%s",arr[i]);
}
scanf("%d", &q);
int counter[q];
char* test[q];
for(i=0; i<q; i++) {
counter[i] = 0;
}
for(i=0; i<q; i++) {
test[i]= malloc(20);
scanf("%s",test[i]);
}
for(i=0; i<n; i++) {
for(j=0; j<q; j++) {
if (strcmp(arr[i],test[j]) == 0) {
counter[j]++;
}
}
}
for(i=0; i<q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
for (i = 0; i < n; i++) {
arr[i] = malloc(20);
scanf("%s", arr[i]);
}
scanf("%d", &q);
char* test[q];
char* s;
int counter[q];
for (i = 0; i < q; i++) {
counter[i] = 0;
}
for (i = 0; i < q; i++) {
test[i] = malloc(20);
scanf("%s", test[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < q; j++) {
if (strcmp(arr[i], test[j]) == 0) {
counter[j]++;
} else {
}
}
}
for (i = 0; i < q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
try this, use varialble after declaration and initialization

Getting a SIGSEGV error in my C program

This is my code for the problem.
I used a dynamic programming approach, and my answer is coming out to be correct. But I am getting a Runtime Error of SIGSEGV, may be because of array index and I am unable to figure out how and where?
If you can figure out what and where is the problem, please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int dynp(int i, int j);
int v[] = {-1, 0, 1, 0};
int h[] = {0, 1, 0, -1};
int ROW, COL;
#define INF 1000000
int minOfTwo(int one, int two){
if (one<two) {
return one;
}else{
return two;
}
}
int matrix[190][190];
int dp[190][190];
int main(int argc, const char * argv[]) {
int t;
scanf("%d", &t);
int i,j;
char s[190];
while (t--) {
scanf("%d %d", &ROW, &COL);
for (i = 0; i<ROW; i++) {
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
dp[i][j] = INF;
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
if (dp[i][j] == INF) {
if (matrix[i][j] == 1) {
dp[i][j] = 0;
}
else{
dp[i][j] = dynp(i, j);
}
}
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
printf("%d ", dp[i][j]);
}
printf("\n");
}
}
return 0;
}
int dynp(int i, int j)
{
if (dp[i][j] != INF) {
return dp[i][j];
}
else{
if (matrix[i][j] == 1) {
dp[i][j] = 0;
return dp[i][j];
}
else{
int k;
for (k = 0; k<4; k++) {
int newi = i + v[k], newj = j + h[k];
if (newi < ROW && newj < COL && newi>=0 && newj>=0) {
dp[i][j] = minOfTwo(dp[i][j], 1 + dynp(newi, newj));
}
}
return dp[i][j];
}
}
}
At the first look, in your code,
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
looks problematic. With the length of s lesser than the value of COL and s being an automatic local variable not initialized explicitly, you'll be accessing allocated but uninitialized memory location.
You should change the looping condition to something like
scanf("%189s", s); //to avoid overflow
int len = strlen(s);
for (j = 0; (j<COL) && (j < len); j++, len--) {
matrix[i][j] = s[j] - 48;

Resources