Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am not getting the desired output for my C program can anyone please have a look on the code and help me to rectify the error ?
Desired output and actual output of my code is:
My Code is:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k;
int blank=0;
int lines = 6;
char symbol='A';
int temp ;
int diff[7]= {0,1,3,5,7,9,11};
k=0;
for(i=lines;i>=0;i--)
{
printf("\n");
symbol = 'A';
for(j=i;j>=0;j--)
printf("%c ",symbol++);
blank = diff[k++];
for(j=0;j<blank;j++)
printf(" ");
symbol = 'F' - (blank/2);
if (blank== 0)
temp = i-1;
else
temp = i;
for(j=0;j<=temp;j++)
printf("%c ",symbol--);
}
getch();
}
Your have written the code in a right way. You have to change the line
for(j=0;j<blank;j++)
printf(" ");
to
for(j=0;j<blank;j++)
printf(" "); // increase one more space
And also try to avoid using void main().Here is your edited code;
#include <math.h>
#include <stdio.h>
int main()
{
int i,j,k;
int blank=0;
int lines = 6;
char symbol='A';
int temp ;
int diff[7]= {0,1,3,5,7,9,11};
k=0;
for(i=lines;i>=0;i--)
{
printf("\n");
symbol = 'A';
for(j=i;j>=0;j--)
printf("%c ",symbol++);
blank = diff[k++];
for(j=0;j<blank;j++)
printf(" ");
symbol = 'F' - (blank/2);
if (blank== 0)
temp = i-1;
else
temp = i;
for(j=0;j<=temp;j++)
printf("%c ",symbol--);
}
return 0;
}
And do not forgot to include headers.
The problem is here
for(j=0;j<blank;j++)
printf(" ");
^
A
You are printing only a single space it should be two.So that one space corresponds to the alphabet and another to the actual space
printf(" ");
^^
A(space)
Firstly, you're printing the wrong number of blanks. And the number you need can be worked out with a small piece of arithmetic.
Secondly, you don't need that messy calculation to work out the first symbol to print out after the blanks because you've already worked it out.
(editted so downvote can be withdrawn)
You need to use more blank spaces
Use 0, 2, 6... so on
Your "diff" array is too small. You're accounting for the missing characters on the left side of the pattern, but not on the right side. Try making the first three elements {0, 2, 6...} and see if that works, and then work on extrapolating the correct pattern from there
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So i have this problem where i have to input a string of unknown size with only lowercase letters then output the number of distinct letters.this is the main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i=-1,j=0,nd=0;
do{
gets(T);
}while((test(T))==1);
do {
i++;
j=i;
do{j++;
}while ((T[i]!=T[j])||((T[j])!=""));
if (T[j]=="")
nd++;
}while (T[i+1]!="");
and this is my function test
int test(char *T){
int i=-1,s;
do {
i++;
}while (((islower(T[i])==1))||(T[i]==""));
if ((T[i]=="")&&(i!=0))
s=0;
else s=1;
return s;
}
the problem is that i get a lot of warnings "comparison between integer and pointer" everytime i compare a char of the array T and i don't knowhow to fix that.your help would be much appreciated.
Update:So i tried fixing the program following your advices and this is the new main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i=-1,j=0,nd=0;
do{
gets(T);
}while((test(T))==1);
do {
i++;
j=i;
do{j++;
}while ((T[i]!=T[j])||((T[j])!='\0'));
if (T[j]=='\0')
nd++;
}while (T[i+1]!='\0');
printf("%d",nd);}
and this is function test
int test(char *T){
int i=-1,s;
do {
i++;
}while (((islower(T[i])==1))||(T[i]=='\0'));
if ((T[i]=='\0')&&(i!=0))
s=0;
else s=1;
return s;
}
I don't get anymore warnings and the program gets compiled with no problems but after i input the string in the execution nothing happens.
You function test should return 1 if the string contains only lowercase letters, and 0 otherwise. Unfortunately, it is not doing that.
You should first test if the character is a letter and then if it's a lowercase letter. Or more efficiently, you test if the character is in the range 'a' to 'z'.
Another problem of your code is the use of do while loops which makes the code difficult to understand and executes the loop once.
Here is a better implementation of the test function:
int test(char *T){
// reject empty strings
if(T[0] == '\0')
return 0;
// reject strings containing non lowercase letter
for(int i = 0; T[i] != '\0'; i++)
if((T[i] < 'a') || (T[i] > 'z'))
return 0;
// string is not empty and contains only lowercase letters
return 1;
Counting the different letters can be made more readable by using a for loop instead of a go while loop.
int nd = 0;
for(int i = 0; T[i] != '\0'; i++) {
for(int j = 0; j < i; j++) {
if(T[j] == T[i])
break; // quit inner loop
nd++;
}
}
This code examine each letter and see if it has been seen before. It is thus different from yours.
A problem in your code is the test (T[i]!=T[j])||((T[j])!='\0'). It should be && instead of ||, and testing if the end of string is reached should be performed first. The test should be (T[j]!='\0')&&(T[i]!=T[j]).
So my code is finally working here's the final main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i,j,nd=0;
do{
gets(T);
}while((test(T))==0);
for(i = 0; T[i] != '\0'; i++) {
j=i;
do{
j++;
}while ((T[j]!='\0')&&(T[j]!=T[i]));
if (T[j]=='\0')
nd++;
}
(#chmike i used the code you posted with a little adjustment on the loop)
and for the function test i used the code that posted #chmike as well.
Huge thanks to all of you guys for the help you provided :)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Here is my program.I am trying to find the frequency of each character of a string and display it. While answering please see to it that I don't want to try the ASCII concept and I want to know whats wrong with this concept.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int l=0,j,k,m,count[10000];
char string[10000];
printf("Enter the string : \n");
scanf("%s",string);
l=strlen(string);
printf("%d",l);
for(j=0;j<l;j++)
{
for(k=j+1;k<l;k++)
{
if(string[j]==string[k])
{
count[j]++;
}
}
}
for(m=0;m<l;m++)
{
printf("%d",count[m]);
}
return 0;
}
So you wish to find the frequency of characters in your string.
About the mistakes in your code:
Consider the string lalal Here you would be counting the last l twice, once corresponding to first l and second time corresponding to third l. Hence your logic is faulty.
Similar is the case of count[]. You haven't initialized the array hence it holds garbage values.
So another approach to your problem could be declaring a 26-element array(English alphabet), iterate through the entire list and increment count corresponding to each element when that element is found.
int frequencyChar[26] = {0};//stores frequency of characters [a-z], initialized to zero
for( i=0; i<strlen(str); i++) //iterate through the entire string
{
frequencyChar[str[i] - 'a']++; //increment count corresponding to each element
}
for( i=0; i<26; i++)
{
printf("%d\n",frequencyChar[i]);
}
P.S.Above code assumes only lowercase characters in string. Minor changes would allow inclusion of uppercase letters!
Here are the problems:
You have written: I am trying to find the frequency of each character, but you code is attempting to calculate histogram of correlations between pairs of characters.
as an index for count you are using j, which iterates over constitutive characters in string. This means that your table count have lots of 0 and only some 1 and nothing else.
So currently this is NOT histogram of pairs of characters nor histogram of characters.
Character histogram can be created like this:
void makeStrHistogram(char *str, int histogram[256])
{
memset(histogram, 0, sizeof(histogram));
while (*str) histogram[*str++]++;
}
void printHistogram(int histogram[256])
{
for (int i=0; i<256; ++i) {
if (histogram[i]) {
printf("%c - %d\n", (char)i, histogram[i]);
}
}
}
To generate character correlation matrix:
void correlationMatrixForStr(char *str, int matrix[256][256])
{
memset(matrix, 0, sizeof(matrix));
int len = strlen(str);
for (int i=0; i<len; ++i) {
for (int j=i+1; j<len; ++j) {
matrix[i][j]++;
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have to write a program that acts like a shell. I wrote the function that gets the input from the user. I also wrote the function that splits it into arguments. The first time I type something, it works well, but the second time, it prints different characters after the ones that I gave it. I don't have to print it in the program. I was just doing it to see if it works correctly. I read a bunch of stuff online, but I can't figure out my error. I suppose it is in makeArgs(), but I can't pinpoint it.
Also, when I give it an input, the readline function adds a \n at the end of the string. I suppose it is from the fact that I press the enter key. I managed to solve the issue, by manually replacing it, but I would like to know if it is normal.
Any help really be appreciated.
Thank You
Screenshot of Xterm after 2 inputs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getText();
int makeArgs();
char *textEntre;
size_t nbCharacters;
char **arguments;
int main (void)
{
while (1){
getText();
int nbArguments = makeArgs();
for(int i =0; i<5; i++){
printf("%s \n",arguments[i]);
}
for(int i=0; i<nbArguments; i++){//free the char ptrs at the end
free(arguments[i]);
}
}
free(textEntre);
free(arguments);
return 0;
}
int getText(){
size_t buffersize = 0;
nbCharacters = getline(&textEntre, &buffersize, stdin);
textEntre[nbCharacters-1] =' '; // when I press enter it regiter the enter as \n so I replace it with a space
return 0;
}
int makeArgs(){
arguments = (char **)malloc(sizeof(char*)*20);
int i;
int j = 0;
int k = 0;
int nbElem = 20; //the number of ptrs that can be in arguments
for(i = 0; i<nbCharacters; i++){
if(i == 20){ //increases the memory allocated if there are more than 20 arguments
nbElem = nbElem *2;
arguments = (char **)realloc(arguments, sizeof(char*)*nbElem);
}
if(textEntre[i] == '"'){ //checks for ""
i++;
while(textEntre[i] != '"'){
i++;
}
}
if(textEntre[i] == ' ' && textEntre[i-1] == ' '){ // eliminates useless spaces
j++;
}
else if(textEntre[i] == ' '){ //save a single argument
char * chptr;
chptr = (char *)malloc(i-j+1); //giving +1 for the \0 at the end
strncpy(chptr, &textEntre[j], i-j);
arguments[k] = chptr;
k++;
j = i +1;
}
}
return k;
}
chptr = (char *)malloc(i-j+1); //giving +1 for the \0 at the end
You properly allocated memory for that terminating \0, but where do you actually add that "\0 at the end"?
strncpy(chptr, &textEntre[j], i-j);
strncpy does not necessarily zero-terminate the destination buffer. You have to do it yourself.
In fact, in this specific application strncpy is a rather inappropriate function: it does not give you anything over ordinary memcpy and might be less efficient. You could just do
memcpy(chptr, &textEntre[j], i - j);
with potentially better efficiency. And, again, don't forget to zero-terminate the destination buffer.
Or you can use sprintf for the same purpose as follows
sprintf(chptr, "%.*s", i - j, &textEntre[j]);
which will produce a properly zero-terminated string in the destination. (Albeit you won't see sprintf used that way very often.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How would one go about adding integers together like this.
Say you start with 1, then you add 2. So you have 12, next add 3, so you have 123. And so on.
I would just concatenate but I'm not allowed to use strings in this program.
Using some unusual math (based on the mechanisms of the decimal system) to make the desired variation of adding:
Code:
#include <stdio.h>
int main(void)
{
int i;
int number=0;
for (i=1; i<5; ++i)
{
number=number*10 + i;
printf("%d\n", number);
}
return 0;
}
Output:
1
12
123
1234
Like this?
#include <stdio.h>
int main() {
int a = 4, b = 5, c = 6, d = 7;
printf("a+b=%d\n",a*10+b);
printf("a+b+c=%d\n",(a*10+b)*10+c);
printf("a+b+c+d=%d\n",((a*10+b)*10+c)*10+d);
return 0;
}
This can be typical case to use realloc
char *mystr = NULL;
char *temp;
char c, ch;
short count = 0;
do {
printf("Enter character : ");
scanf(" %c", &c); // Better use sscanf or fgets, scanf is problematic
temp = (char*)realloc(mystr, (++count) * sizeof *mystr);
if (NULL != temp) {
mystr = temp;
mystr[count - 1] = c;
}
printf("Do you wish to continue: ");
scanf(" %c", &ch);
} while ('y' == ch);
// Since and since you don't have a null terminated string, do
for (int i = 0; i < count; i++)
printf("%c", mystr[i]);
printf("\n");
free(mystr); // Freeing the memory
getch();
Note : And you don't have strings in this program ;)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a program of string pallindrome, code is compiling successfully but on running it accepting the string but nothing after that, the output window stays on hold with cursor blinking, help me what is wrong with this code.
I am using dev-c++
gets(ch); // the program stops here
p=ch;
while(ch!='\0')
{ p++;
size++;
}
for(i=0;i<20;i++)
{
if(c[i]==c[j])
printf("string is pallindrome");
else printf("string is not pallindrome");
}
getch();
return 0;
}
Here is the problem:
while(ch!='\0')
ch is a char array, and you are comparing it with a single char.
Also, size is not initialised.
I would suggest something like this:
size=0;
while(ch[size]!='\0')
{ p++;
size++;
}
or, using the pointer method:
while(*p!=0)
{
p++;
size++;
}
Also, instead of printing inside the for loop (which would make it print several times), use a flag variable.
You only need one loop, for example while (i < i).
Look at this example that will do the job:
#include <stdio.h>
#include <string.h>
/* in c99 use <stdbool.h> instead*/
typedef int bool;
#define true 1;
#define false 0;
int main(void)
{
char ch[20];
puts("enter the string: ");
gets(ch);
size_t size = strlen(ch);
bool pallindrome = true;
int j = size-1;
int i = 0;
while (i < j)
{
if(ch[i] != ch[j]) {
pallindrome = false;
break;
}
++i;
--j;
}
if (pallindrome)
printf("\"%s\" is pallindrome\n", ch);
else
printf("\"%s\" is not pallindrome\n", ch);
getch();
return 0;
}