Printing Staircase pattern using only one loop - c

enter image description here
I thought about printing this shape only using one loop but I didn't make it and I want to know if it's possible and if it's possible what should I do ?
Here is my code:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 16; i++) {
if (i % 4 == 0) {
printf("\n");
printf(" ");
}
else
printf("*");
}
}

Having only allowing one for loop for the task seems like an artificial constraint, but with that in mind, utilizing some string functions would allow you to perform that task. Following is a snippet of code utilizing just one for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 24
int main()
{
char gap[MAX];
char * stair = "***";
strcpy(gap, "");
for (int i = 0; i < (MAX / 3) - 1; i++)
{
printf("%s%s\n", gap, stair);
strcat(gap, " ");
}
return 0;
}
Following was the sample output at the terminal.
#Dev:~/C_Programs/Console/Stairs/bin/Release$ ./Stairs
***
***
***
***
***
***
***
See if that meets the spirit of your project.

Related

How to find duplicate letter in array in C

I am making a program which requires the user to input an argument (argv[1]) where the argument is every letter of the alphabet rearranged however the user likes it. Examples of valid input is "YTNSHKVEFXRBAUQZCLWDMIPGJO" and "JTREKYAVOGDXPSNCUIZLFBMWHQ". Examples of invalid input would then be "VCHPRZGJVTLSKFBDQWAXEUYMOI" and "ABCDEFGHIJKLMNOPQRSTUYYYYY" since there are duplicates of 'V' and 'Y' in the respective examples.
What I know so far is, that you can loop through the whole argument like the following
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
//Place something in here...
}
However, I do not quite know if this would be the right way to go when looking for duplicates? Furthermore, I want the answer to be as simple as possible, right know time and cpu usage is not a priority, so "the best" algorithm is not necessarily the one I am looking for.
Try it.
#include <stdio.h>
#include <stddef.h>
int main()
{
char * input = "ABCC";
/*
*For any character, its value must be locate in 0 ~ 255, so we just
*need to check counter of corresponding index whether greater than zero.
*/
size_t ascii[256] = {0, };
char * cursor = input;
char c = '\0';
while((c=*cursor++))
{
if(ascii[c] == 0)
++ascii[c];
else
{
printf("Find %c has existed.\n", c);
break;
}
}
return 0;
}
assuming that all your letters are capital letters you can use a hash table to make this algorithm work in O(n) time complexity.
#include<stdio.h>
#include<string.h>
int main(int argc, char** argv){
int arr[50]={};
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
arr[argv[1][j]-'A']++;
}
printf("duplicate letters: ");
for(int i=0;i<'Z'-'A'+1;i++){
if(arr[i]>=2)printf("%c ",i+'A');
}
}
here we make an array arr initialized to zeros. this array will keep count of the occurrences of every letter.
and then we look for letters that appeared 2 or more times those are the duplicated letters.
Also using that same array you can check if all the letters occured at least once to check if it is a permutation
I don't know if this is the type of code that you are looking for, but here's what I did. It looks for the duplicates in the given set of strings.
#include <stdio.h>
#include <stdlib.h>
#define max 50
int main() {
char stringArg[max];
int dupliCount = 0;
printf("Enter A string: ");
scanf("%s",stringArg);
system("cls");
int length = strlen(stringArg);
for(int i=0; i<length; i++){
for(int j=i+1; j<length; j++){
if(stringArg[i] == stringArg[j]){
dupliCount +=1;
}
}
}
if(dupliCount > 0)
printf("Invalid Input");
printf("Valid Input");
}
This code snippet is used to count the duplicate letters in the array.
If you want to remove the letters, Also this code snippet is helpful .Set null when the same characters are in the same letter.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count=0;
int array[]={'e','d','w','f','b','e'};
for(int i=0;i<array.Lenth;i++)
{
for(int j=1;j<array.Length;j++)
{
if(array[i]==array[j])
{
count++;
}
}
}
printf("The dublicate letter count is : %d",count);
}

Error in c when iterating: use of undeclared identifier i

I am creating a program that will ask a command line argument from the user, and the user need to input only integers as the argv[1]. It should reject any input other than integers. My code is as below:
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[] )
string s = argv[1];
for (int i = 0; n = strlen(s); i< n; i++)
{
if(!( isdigit(s[i])) )
{
printf("All numbers: correct input");
return 1;
}
}
//Else print a prompt asking for a plaintext to cipher
else
{
string p = get_string("Your text here: ");
return 0;
}
}
Running the above code throws me an error: error: use of undeclared identifier 'i' for (int i = 0; n = strlen(s); i< n; i++)
Where do I do wrong here and how do I fix this? Thanks.
Sorry if my questions seem stupid, I am still a newbie learning here and know absolutely nothing about C before. Thanks for the help though.
If you have a C89 compiler you will need to put the delarations at the top of the scope block, so the variable i has to be declared before the for loop.
Try this if your using C89:
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[] ){
string s = argv[1];
int i = 0;
int n = strlen(s);
for (; i< n; i++)
{
if(!( isdigit(s[i])) )
{
printf("All numbers: correct input");
return 1;
}
}
//Else print a prompt asking for a plaintext to cipher
else
{
string p = get_string("Your text here: ");
return 0;
}
} //end of for loop? TODO: fix your braces spacing the sample was broken
}//end of for main? TODO: fix your braces spacing the sample was broken

how to clear my previous output in Linux terminal using C?

This is my sample program for simulating a Loading progress. The only problem I'm facing right now is clearing my previous output of "Loading: %i"
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < 5; ++i) {
j = i;
printf("Loading: %i%%", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}
if you print \r instead of \n using printf, then it will return to the beginning of the same line instead of the next line. Then you can re-print the new status on top of the old line.
Anyway here is the correct code. Thanks to Mobius and Dmitri.
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < TIMELIMIT; ++i) {
j = i;
printf("Loading: %i%%", ++j);
printf("\r");
fflush(stdout);
sleep(1);
}
return(0);
}
I think the answer to this question is system("reset").
This is the command you're looking for
reset
this command clears your whole terminal.
and your code go like this,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
int i,j;
for(int i = 0, j; i < 5; ++i) {
j = i;
system("reset");
printf("Loading: %i%%\n", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}

Segmentation fault (core dumped) programming in C

I was solving the HackerRank problem given here : -- https://www.hackerrank.com/challenges/bigger-is-greater
Program statement is as follow :
Given a word, rearrange the letters to construct another word in such a way that is lexicographically greater than original one. In case of multiple possible answers, find the lexicographically smallest one among them.
If you don't understand then just go to the link; they have explained with examples.
I made the program as given below. In this program I have made two dimensional array. And variable t decides number of row and number is fix.
Code is running as it should be when t = 1.
But when t is greater than 1 or some large number it gives error segmentation error
Code is as below :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i,j,n,rot;
int t;
scanf("%d",&t);
char c[t][100];
char temp;
for(int i=0;i<t;i++)
{
scanf(" %s",c[i]);
}
rot=t;
for(int t=0;t<rot;t++)
{
n = strlen(c[t]);
//printf("%d\n",n);
for(i=n-1;i>=0;i--)
{
for(j=i-1;j>=0;j--)
{
//printf("comparint %c and %c\n",c[t][i],c[t][j]); //FOR DEBUG
if(c[t][i]>c[t][j]) goto gotit;
}
}
printf("no answer\n");
continue;
gotit:
temp = c[t][i];
c[t][i]=c[t][j];
c[t][j]=temp;
n = (n-1)-j;
//printf("%s\n",c[t]); //FOR DEBUG
//printf("%d %d %d\n",i,j,n); //FOR DEBUG
for(i=0;i<n-1;i++)
{
for(int k=0;k<n-1;k++)
{
// printf("comparint %c and %c\n",c[t][j+k+1],c[t][j+k+2]);
if(c[t][j+k+1]>c[t][j+k+2] )
{
temp = c[t][j+k+1];
c[t][j+k+1]=c[t][j+k+2];
c[t][j+k+2]=temp;
}
}
}
printf("%s\n",c[t]);
}
return 0;
}
t can be 10^5 or 100,000. Your c array is c[t][100], so the size of that is 100000 * 100, which is 10,000,000. You're probably getting a stack overflow.
As WhozCraig pointed out, the processing of each case is independent. Thus, c can be a one dimensional array: char c[100]. Change all c[t][...] into c[...].
Adjust things so that you have one outer loop:
int
main()
{
int t;
char c[100];
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf(" %s", c);
// do all processing for this line ...
n = strlen(c);
}
return 0;
}

array of string output is a clover symbol

No matter what the 5th input the output was clover symbol, program purpose was to align right the inputs:
EDIT
im not using scanf(%[\^n],a[i]), the output was horrible, using gets instead
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char a[6][50];
int i;
for (i=1;i<=5;i++)
{
printf("insert name%d :\n",i);
gets(a[i]);
}
for (i=1;i<=5;i++)
{
printf("%d%25s\n",i,a[i]);
}
return 0;
}
Example output:
1 far cry
2 iron man
3 new super mario
4 program
5 "clover"
Using your code, I got strange chars on output as you mentioned.
So, I made some fixes on your code and now I think it works properly.
#include <unistd.h>
int main(void)
{
char a[6][50];
int i, r;
for (i = 1; i <= 5; i++) {
printf("insert name%d :\n", i);
r = read(STDIN_FILENO, a[i], 49);
a[i][r] = '\0';
}
for (i = 1; i <= 5; i++) {
printf("%d%25s\n", i, a[i]);
}
return 0;
}
Avoid to use deprecated functions in your code.
You need to ensure that after reading from stdin, your buffer is going to get the null terminator right after the last position written in buffer.

Resources