Somebody know how to make char str[20]="reviver",palind=1 be true printf("%s eh palindromo.\n",str)?
My code below
#include <stdio.h>
#include <string.h>
int main()
{
char str[20]="reviver",palind=1;
int tam = strlen(str);
int i=0; char cont = tam;
for(; i <= cont; i++,cont--)
{
if(str[i]!=str[cont])
{
palind=0;
break;
}
cont--;
}
if(palind)
printf("%s eh palindromo.\n",str);
else
printf("%s nao eh palindromo.\n",str);
return 0;
}
I hope if you do two corrections in line numbers 7 and 15 as below, you may get expected result.
#include <stdio.h>
#include <string.h>
int main()
{
char str[20]="reviver",palind=1;
int tam = strlen(str);
int i=0; char cont = tam-1;
for(; i <= cont; i++,cont--)
{
if(str[i]!=str[cont])
{
palind=0;
break;
}
/*cont--;*/
}
if(palind)
printf("%s eh palindromo.\n",str);
else
printf("%s nao eh palindromo.\n",str);
return 0;
}
Related
I'm beginer in C/C++ programming.
This is my program that displays binary numbers in ascending order in the terminal (I'm compiling in Linux Mint).
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
int main()
{
unsigned int bitCount;
unsigned int naborCount;
printf("Число битов в наборе: ");
scanf("%d", &bitCount);
printf("\n");
naborCount = pow(2, bitCount);
char naborStr[bitCount*2];
for(int i = 0; i<naborCount; i++)
{
for(int j = 0; j<bitCount; j++)
{
if((i & (1<<j))==0)
{
strcat(naborStr, "0 ");
}
else
{
strcat(naborStr, "1 ");
}
if(j == bitCount-1)
{
reverse(naborStr, 0, strlen(naborStr)-1);
printf("%s \r\n", naborStr);
memset(naborStr, 0, sizeof(naborStr));
}
}
}
return 0;
}
This is what I see in the terminal
Where did this symbol come from? How to solve it?
C strings are null terminated.
The %s specifier searches for a null termination.
In your case it keeps on printing until it finds one, so you get some random symbols.
Try making use of null character at the end of the string and check.
Have a look at the following implementation:
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
int main()
{
unsigned int bitCount;
unsigned int naborCount;
printf("Число битов в наборе: ");
scanf("%d", &bitCount);
printf("\n");
naborCount = pow(2, bitCount);
char naborStr[bitCount*2 + 1]; //Increased size by 1 for null character
for(int i = 0; i<naborCount; i++)
{
for(int j = 0; j<bitCount; j++)
{
if((i & (1<<j))==0)
{
strcat(naborStr, "0 ");
}
else
{
strcat(naborStr, "1 ");
}
if(j == bitCount-1)
{
reverse(naborStr, 0, strlen(naborStr)-1);
naborStr[bitCount*2 +1] = '\0'; //Appending null character
printf("%s \r\n", naborStr);
memset(naborStr, 0, sizeof(naborStr));
}
}
}
return 0;
}
i have to read from a file which contains names and scores the results of a contest but the function fscanf is not working properly.. Does it have to do with newlines or something?
I'll leave below the code and the screenshots of the issue i'm having.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct contest
{
char name[25];
int score;
}Contest;
int main(int argc, const char * argv[]) {
Contest players[100];
FILE *fp;
fp = fopen("contest.txt", "r");
if(fp == NULL)
{
fprintf(stderr, "Error: failed to open the file\n");
exit(EXIT_FAILURE);
}
int cnt = 0;
while(fscanf(fp, "%s %d", players[cnt].name, &players[cnt].score) != EOF)
{
++cnt;
}
char temp[25];
for(int t = 0; t < cnt; ++t)
{
for(int u = 0; u < cnt; ++u)
{
if(strcasecmp(players[t].name, players[u].name) > 0)
{
strcpy(temp, players[t].name);
strcpy(players[t].name, players[u].name);
strcpy(players[u].name, temp);
}
}
}
for(int t = 0; t < cnt; ++t)
{
fprintf(stdout, "Name : %s Score : %d\n", players[t].name, players[t].score);
}
fclose(fp);
return 0;
}
Input:
Randal 34
Leonel 67
Vaughn 100
Missy 68
Cristopher 92
Dagmar 102
Blondell 88
Milly 83
Darrel 12
Josh 71
Output:
You've just made two little mistakes: (1) forgot to also swap the scores and (2) did the wrong comparison in the strcasecmp. For the rest, it's all working fine here.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct contest
{
char name[32];
int score;
} Contest;
int main()
{
Contest players[100];
FILE *fp = fopen("contest.txt", "r");
assert(fp);
int cnt = 0;
while(fscanf(fp, "%s %d", players[cnt].name, &players[cnt].score) != EOF)
++cnt;
for(int i = 0; i < cnt; ++i)
{
for(int j = 0; j < cnt; ++j)
{
if(strcasecmp(players[i].name, players[j].name) < 0)
{
int tempScore = players[i].score;
players[i].score = players[j].score;
players[j].score = tempScore;
char tempName[32];
strcpy(tempName, players[i].name);
strcpy(players[i].name, players[j].name);
strcpy(players[j].name, tempName);
}
}
}
for(int i = 0; i < cnt; ++i)
fprintf(stdout, "Name : %s Score : %d\n", players[i].name, players[i].score);
fclose(fp);
return 0;
}
I tried making a small program that would detect if you typed in a palindrome but for some reason, it just loops
ps I'm a beginner
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[100], arr1[100];
int i;
printf("type in a string\n\n");
gets(arr);
strrev(arr) == arr1;
for (i=0; arr==arr1; i++)
{
printf("%c is a palindrome\n", arr);
}
for (i=0; arr!=arr1; i++)
{
printf("%c is not a palindrome\n", arr);
}
return 0;
}
arr and arr1 are base address of the two arrays respectively which would be different.One simple Code is here
#include <stdio.h>
#include <string.h>
int main(void)
{
char arr[100], arr1[100];
int i;
printf("type in a string\n\n");
gets(arr);
int len=strlen(arr);
strcpy(arr1,arr);
strrev(arr);
for(i=0;i<len;i++){
if(arr1[i]!=arr[i]){
printf("Not palindrome");
return 1;
}
}
printf("Palindrome");
return 0;
}
Use fgets instead of gets.
The first character could be compared to the last character. Then move the indexes toward the center for subsequent comparisons.
#include <stdio.h>
#include <string.h>
int main(void) {
char arr[100] = "";
int first = 0;
int last = 0;
printf ( "type in a string\n\n");
if ( !fgets ( arr, sizeof arr, stdin)) {
printf ( "fgets problem\n");
return 0;
}
arr[strcspn ( arr, "\n")] = '\0';//remove newline
for ( first = 0, last = strlen ( arr) - 1; first <= last; first++, last--) {
if ( arr[first] != arr[last]) {
printf("%s is not a palindrome\n", arr);
return 0;
}
}
printf ( "%s is a palindrome\n", arr);
return 0;
}
I just have written a program which suppose to return char which appears the most/least. Program work during the testing without switch statement, but when I added it start to crash. Can you have a look?
Main function
#include <stdio.h>
#include <stdlib.h>
#include "tools.h"
int main(int argc, char *argv[]) {
int count[256] = { 0 };
int c;
while ( (c=getchar())!=EOF ){
count[c]++;
}
switch (argv[1][1]) {
case 'm': case 'M':
mostOften(count);
break;
case 'l': case 'L':
leastOften(count);
break;
default:
mostOften(count);
break;
}
return 0;
}
Tools function
#include <stdio.h>
#include <stdlib.h>
#include "tools.h"
void mostOften(int *s) {
int j;
int max, cha;
for(j=32; j<126; j++){
if(s[j]>max) {
max=s[j];
cha=j;
}
}
printf("char %c: %d times\n", cha, max);
}
void leastOften(int *s) {
int j;
int min=10000, cha;
for(j=32; j<126; j++){
if(s[j] && s[j]<=min) {
min=s[j];
cha=j;
}
}
printf("char %c: %d times\n", cha, min);
}
You are using max uninitialized, thus, reading garbage:
int max, cha;
for(j=32; j<126; j++){
if(s[j]>max) {
Also, you need to check if argv[1][1] exists before using it:
switch ((argc > 1 && argv[1][0]) ? argv[1][1] : 0) {
I'm pretty new to C so be gentle, somewhy my code doesn't work, be kind and help my figure it out why it doesn't, also if it's possible to make it shorter without making it too complicated, please help in that too.
main.c
#include <stdio.h>
#include <stdlib.h>
#include "swap.h"
#include "magic.h"
int main(int argc, char *argv[])
{
int i,j,count;
int min=atoi(argv[1]);
int max=atoi(argv[2]);
if(min>max)
{
swap(&min, &max);
}
if (min<0)
{
min=1;
}
if(argc<2 || argc>5){exit(EXIT_FAILURE);}
else
{
magic();
}
}
Magic.c
#include <stdlib.h>
#include <stdio.h>
#include magic.h
magic(char *argv[])
{
for(i = min; i<=max; i++)
{
count = 0;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0 && i!= 1 && i!= 0)
printf("%d \n",i);
}
return 0;
}
this is as single file for simplicity
#include <stdio.h>
#include <stdlib.h>
void swap(int *a,int *b){
int c=*a;
*a=*b;
*b=c;
}
void magic(int min, int max);
int main(int argc, char *argv[])
{
int min,max;
if(argc!=3) // (argc<2 || argc>5)
exit(EXIT_FAILURE);
min=atoi(argv[1]);
max=atoi(argv[2]);
if(min>max)
swap(&min, &max);
if (min<0)
min=1;
magic(min,max);
return EXIT_SUCCESS;
}
void magic(int min, int max){
int i,j,count;
for(i = min; i<=max; i++){
count = 0;
for(j=2; j<=i/2; j++) {
if(i%j==0){
count++;
break;
}
}
if(count==0 && i!= 1 && i!= 0)
printf("%d \n",i);
}
}
You have some error in your code :
you are trying to use the variables min, max and countin your function magic however since they are declared in your main function magic does not have knowledge of them.
you should change your main function like :
#include <stdio.h>
#include <stdlib.h>
#include "swap.h"
#include "magic.h"
int main(int argc, char *argv[])
{
int min=atoi(argv[1]);
int max=atoi(argv[2]);
if(min>max)
{
swap(&min, &max);
}
if (min<0)
{
min=1;
}
if(argc<2 || argc>5){exit(EXIT_FAILURE);}
else
{
magic(min, max);
}
return EXIT_SUCCESS;
}
and your magic function :
#include <stdlib.h>
#include <stdio.h>
#include "magic.h"
int magic(int min, int max)
{
int count;
for(int i = min; i<=max; i++)
{
count = 0;
for(int j=2; j<=i/2; j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0 && i!= 1 && i!= 0)
printf("%d \n",i);
}
return 0;
}
In the main function I change magic(); by magic(min, max); to pass parameters to the function which can be retrieve in magic with it's new signature int magic(int min, int max).
Since your magic function return an integer don't forget to specify the return value in the signature of your function.