Longest palindrome in a string and it's length - c

I have a program that is supposed to go through a string identify possible palindromes, check if it is a palindrome and then return the length from palindromelength() or -1 if it is not, and print out the longest palindrome of the string.
The program is compiling but the output is wrong.
This is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int palindromelength(char *str, int i, int j){
char *start = str;
char *end = str + j;
while(start < end){
if(*start != *end)
return -1;
start++;
end--;
}
return j-i+1;
}
void ispalindrome(char *str){
int length = 1, max = 1, start =0, i, j;
for(i=0; i<= strlen(str) -2; i++){
for(j= 1; j <= strlen(str)-1; j++){
if(abs(i-j) > 1){
length = palindromelength(str, i, j);
if( length> max){
max = length;
start = i;
}
}
}
}
if(max > 1){
printf("Largest palindrome is ");
for( ; start <= j; start++){
printf("%c", str[start]);
start++;
}
}
else
printf("No palindromes in string.");
}
int main(void) {
char a[50];
char *a2;
printf("Enter a string\n");
scanf("%[^\n]", a);
int length = strlen(a) + 1;
a2 = (char*)malloc(length*sizeof(char));
strcpy(a2, a);
free (a2);
char *a3 = &a;
ispalindrome(a3);
return 0;
}
I have tried the palindromelength() separately with a simple string, "aracecar". palindrimelength(a3, 0, 4) returns -1 so that is right, palindromelength(a3, 0, 3) returns 3 so that is right, but palindromelength(a3, 1, 7) returns -1, which is wrong. I double checked my function with other ones on stack overflow and it seems right, what could be the problem?
As for the second function ispalindrome() is there a better way I could write that one? It just seems kind of messy right know.
I am a newbie, therefore I may not have yet learned some more advanced/sophisticated variations I could take to solve this.

I presume int i is the starting index of the string and int j is the ending index. If that is so, have a closer look at the first line of your palindromelength() function. Could the function ever start from somewhere other than index 0?
As for ways to do the ispalindrome() function, there are many algorithms out there to check whether or not an input is a palindrome, I would suggest having a look around and finding out different methods. If your way works, great! probably the cleanest way is to use recursion although that can take some thinking.

Related

Every k-th digit cyclic problem using strings in C

Given some number in a form of string, I want to extract every k-th number from it. Then I go through the remaining string and extract every k-th number again. The thing I get as a result should be the number formed by these extracted ones(in a proper order). Example: 123456789, k = 3 --> 369485271
My algorithm is as follows: While the lenght of the string allows extracting every k-th number, I go through the string and store every k-th element in another string. Then I delete the extracted elements from the original string by tracking the proper index of an element and proceed forvard while the lenght of my str is sufficient.
I can't figure out what's the problem with my code. And maybe my approach isn't that good and there are some better/simpler ways of diong this?
#include <stdio.h>
#include <string.h>
void remove(char *str, unsigned int index) {
char *src;
for (src = str+index; *src != '\0'; *src = *(src+1),++src) ;
*src = '\0';
}
int main() {
char number[100];
char result[100];
int k;
printf("Enter a string: ");
scanf("%s",number);
printf("Enter a key: ");
scanf("%d",&k);
while (strlen(number)>k-1) {
for (int i = 0, p = 0; number[i] != '\0'; i++) {
if (i % k == (k-1)) {
result[p] = number[i];
p++;
}
}
for (int j = 0; number[j] != '\0'; j++){
if (j % k == (k-1)) {
remove(number, j);
j+=1; /*since the index was shifted due to removing an element*/
}
}
}
puts(result);
return 0;
}
You some issues:
You start writing your output from scratch again in each iteration of your while loop.
You do not handle the last digits
You do not treat the input as a cyclic input.
You do not terminate your output string.
remove is already a name of standard library function.
A shorter version could be this (untested):
#include <stdio.h>
#include <string.h>
void remove_digit(char *str, unsigned int index) {
char *src;
for (src = str+index; *src != '\0'; *src = *(src+1),++src)
;
}
int main() {
char number[100];
char result[100];
int k;
printf("Enter a string: ");
scanf("%s",number);
printf("Enter a key: ");
scanf("%d",&k);
int p = 0;
int i = 0;
int skip = k-1; // We remove 1 digit and skip k-1 digits
while (number[0] != 0) {
i = (i + skip) % strlen(number);
result[p] = number[i];
p++;
remove_digit(number, i);
}
number[p] = 0;
puts(result);
return 0;
}
The following code seems to be what you want:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void remove_(char *str, unsigned int index) {
char *src;
for (src = str+index; *src != '\0'; *src = *(src+1),++src) ;
*src = '\0';
}
int main(int argc, const char * argv[]) {
char number[100];
char result[100];
int tmp[100];
int k;
printf("Enter a string: ");
scanf("%s",number);
printf("Enter a key: ");
scanf("%d",&k);
int p = 0;
for (int tp = 0; strlen(number) > k-1; tp = 0) {
for (int i = 0; number[i] != '\0'; i++)
if (i % k == (k-1))result[p++] = number[i];
for (int j = 0; number[j] != '\0'; j++)
if (j % k == (k-1)) tmp[tp++] = j;
for (; tp; --tp) remove_(number, tmp[tp-1]);
}
// The newly added code
for (int index; strlen(number); ) {
index = (k-1) % strlen(number);
result[p++] = number[index];
remove_(number, index);
}
puts(result);
return 0;
}
The most important thing is that every while loop, you need to remove the elements in number at once. While ensuring the integrity of your original code, I made some changes. Unfortunately, the main idea of ​​the original code is wrong.
It should circulate from the tail (including the rest) to the head after one round. But I found that the function of the code you provided is that after each round, the next round starts from the 0th element of the head.
By the way, your algorithm is similar to the Josephus problem

Why my program does not print true result in C

I've found a code about finding longest string in an array and print it with strrev(); function.
My code works successfully, but if my first string was larger than other strings my program does not understand.
This is my prototype:
void LongStr(char str[][50],int n);
and This is my Main() function:
int main()
{
char str[size][wsize];
int n;
printf("\n How many words to accept:- ");
scanf("%d",&n);
printf("\n Enter %d words:- \n \n",n);
for(int i = 0; i < n; i++)
scanf("%s", str[i]);
LongStr(str,n);
return 0;
}
and this is my LongStr(); function:
void LongStr(char str[][50],int n)
{
int Max = strlen(str[0]);
int len;
int c;
for(int i = 1; i < n; i++)
{
len = strlen(str[i]);
if( len > Max )
{
c = i;
Max = len;
}
}
printf("\n The longest string with reverse model is: \"%s\" \n \n",strrev(str[c]));
}
Example:
FirstString
SecondLongestString
ThirdString
output: strrev("SecondLongestString");
Example:
FirstString
SecondString
ThirdLongestString
output: strrev("SecondLongestString");
Example:
FirstLongestString
SecondString
ThirdString
output: crash!
Thanks.
If the longest string is the first c is never initialized, which makes it garbage and most likely a huge number to index the array.
You should initialize it to 0:
void LongStr(char str[][50],int n)
{
int Max = strlen(str[0]);
int len;
int c = 0;
// ...
}
PS: although this problem doesn't occur with len, I'd say it's probably good practise to initialize it to something too. In other words, initializing all variables is a good thing. However, I haven't code in C in a long time and I'm not sure if this is the norm.
Really simple it's just a missing piece in your code
int Max = strlen(str[0]);
int len;
int c;
You need to give the c a value that why it's not working because it doesn't have a base value so try to change your code to
int Max = strlen(str[0]);
int len;
int c = 0; // base value of 0

Reverse string with external function

I recently started a course in Programming in C. I got an assignment where I'm going to make an external function with this declaration:
void reverse(char *array, int numberOfChars)
My code looks like this at the moment. The problem is that the code will just reverse the first half of the word, and keep the second half as normal.
#include <stdio.h>
#include <string.h>
void reverse(char *array, int numberOfChars) {
int i = 0;
int j = 0;
for (i = numberOfChars - 1; i >= 0; i--) {
array[j] = array[i];
j = j + 1;
}
}
#define NAMESIZE 100
int main() {
char word[NAMESIZE];
int length;
printf("Enter a word: ");
scanf("%s", word);
getchar();
length = strlen(word);
reverse(word, length);
printf("%s\n", word);
}
Even when you use an intermediate variable, your code will reverse the string twice, leaving it as it was. A small change to the code compares i with j as well as using a temporary variable to do the swap.
int i, j = 0;
char temp;
for(i = numberOfChars-1; i > j; i--){
temp = array[j];
array[j] = array[i];
array[i] = temp;
j = j + 1;
}
Your function has a classic problem: you are reversing the string in place. You overwrite the characters at the start of the string with the characters from the end. The initial characters are lost by the time you reach the mid point. You should instead swap the characters from both ends and stop at the mid point.
You are going to need a secondary char array to reverse the string.Try like this:
#include <stdio.h>
#include <string.h>
#define NAMESIZE 100
void reverse(char *arr,int numberOfChars);
int main()
{
char word[NAMESIZE];
printf("Enter a word: ");
scanf("%s",word);
getchar();
size_t length=strlen(word);
reverse(word,length);
printf("%s\n",word);
}
void reverse(char *arr,int numberOfChars)
{
char buffer[NAMESIZE];
int j = 0,i = numberOfChars;
while( i-- )
buffer[j++] = arr[i];
buffer[j] = '\0';
strcpy(arr,buffer);
}
A simple way to prevent overwriting your array is to copy your array into a temporary one and write the reversed readed temporary array into the other.
If you want to have an unknown array size at compilation to reverse, I recommend you to use a heap array (see man malloc) instead of stack one to prevent segmentation fault if numberOfChars is too hight.

Table initialization fails with a variable

I need to make a console program that prints the following output :
aaaaaaaaa
abbbbbbba
abcccccba
abcdddcba
abcdedcba
abcdddcba
abcccccba
abbbbbbba
aaaaaaaaa
So I made the following code, wich seems to work :
#include <stdio.h>
#include <string.h>
int main()
{
int c, i = 0, p;
scanf("%d", &c);
int len = c*2-1;
printf("%d\n", len);
char ligne[9];
while (i < len-1){
p = 0;
for (int j = 0; j < c; j++){
ligne[len-1-j] = p+97;
ligne[j] = p+97;
if (j < c && p < i)
p++;
}
printf("%s\n", ligne);
i++;
}
return 0;
}
Which seems to work, but when I replace :
char ligne[9];
By :
char ligne[len];
I obtain the following output :
Your program output contains an invalid UTF8 character.
It seems that the table somehow "expanded" : there are much more than 9 fields in it.
I know that initializing tables with variables aren't allowed in some versions of C, but it ain't a problem for me. so, does anyone know where the prolem come from ?
Your problem is that you're trying to store 9 characters plus a terminal null into the space for 9 characters; this doesn't work.
Remove the - 1 from your calculation of len and ensure that the string is null terminated.
It was only by accident it worked with the fixed length array.
You could also fix the problem by revising the printf() statement to:
printf("%.9s\n", ligne);
or:
printf("%.*s\n", len, ligne);
This doesn't require a null-terminated string any more.
The code in the question prints the top half of the output, but gets stuck printing the same line over and over again on the bottom half of the output. This code fixes that, using the min() inline function to determine the maximum increment to be shown. It also validates the return value from scanf() to ensure that the value in c is reasonable (1..26) — failing silently if it is not OK.
#include <stdio.h>
#include <string.h>
static inline int min(int i, int j) { return (i < j) ? i : j; }
int main(void)
{
int c;
if (scanf("%d", &c) == 1 && c >= 1 && c <= 26)
{
int len = c*2-1;
printf("%d\n", len);
char ligne[len];
for (int i = 0; i < len; i++)
{
int p = 0;
int maxinc = min(len-1-i, i);
for (int j = 0; j < c; j++)
{
ligne[len-1-j] = p + 'a';
ligne[j] = p + 'a';
if (p < maxinc)
p++;
}
printf("%.*s\n", len, ligne);
}
}
return 0;
}
You have to allocate 1 char extra space and explicitely add a NULL terminator to the array ligne.
char ligne[len + 1];
ligne[len] = '\0';
char ligne[len+1];
for(int i=0;i<sizeof(ligne);++i)
ligne[i]=0;

C - Find most frequent element in char array

i'm developing a little function to display the most frequent character in a (char) array.
This is what I've accomplished so far, but I think i'm on the wrong way.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char test[10] = "ciaociaoci";
max_caratt(test, 10);
}
int max_caratt(char input[], int size)
{
int i;
char max[300];
max[0] = input[0];
for (i=0; i<size; i++)
{
if(strncmp(input,input[i],1) == 1)
{
printf("occourrence found");
max[i] = input[i];
}
}
}
Any help?
Actually, the correct code is this.
It's just a corrected version of IntermediateHacker's below snippet.
void main()
{
int array[255] = {0}; // initialize all elements to 0
char str[] = "thequickbrownfoxjumpedoverthelazydog";
int i, max, index;
for(i = 0; str[i] != 0; i++)
{
++array[str[i]];
}
// Find the letter that was used the most
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
if( array[str[i]] > max)
{
max = array[str[i]];
index = i;
}
}
printf("The max character is: %c \n", str[index]);
}
The easiest way to find the most common character is to create an int array of 255 and just increment the arraly element that corresponds to the character. For example: if the charcter is 'A', then increment the 'A'th element (if you look at any ascii table you will see that the letter 'A' has a decimal value of 65)
int array[255] = {0}; // initialize all elements to 0
char str[] = "The quick brown fox jumped over the lazy dog.";
int i, max, index;
// Now count all the letters in the sentence
for(i = 0; str[i] != 0; i++)
{
++array[str[i]];
}
// Find the letter that was used the most
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
if( array[i] > max)
{
max = array[i];
index = i;
}
}
printf("The max character is: %c \n", (char)index);
You're passing a (almost) string and a char to strncmp(). strncmp() takes two strings (and an integer). Your program shouldn't even compile!
Suggestion: increase the warning level of your compiler and mind the warnings.
You may want to look at strchr() ...
Assuming an input array of 0-127, the following should get you the most common character in a single pass through the string. Note, if you want to worry about negative numbers, shift everything up by +127 as needed...
char mostCommonChar(char *str) {
/* we are making the assumption that the string passed in has values
* between 0 and 127.
*/
int cnt[128], max = 0;
char *idx = str;
/* clear counts */
memset((void *)cnt, 0, sizeof(int) * 128);
/* collect info */
while(*idx) {
cnt[*idx]++;
if(cnt[*idx] > cnt[max]) {
max = *idx;
}
idx++;
}
/* we know the max */
return max;
}
If you don't need to preserve the input array, you could sort the input array first, then find the longest contiguous run of a single character. This approach is slower, but uses less space.
I made a working version using structs. It works fine, I guess, but I think there's a MUCH better way to write this algorithm.
#include <stdio.h>
#include <stdlib.h>
struct alphabet {
char letter;
int times;
};
typedef struct alphabet Alphabet;
void main() {
char string[300];
gets(string);
Alphabet Alph[300];
int i=0, j=0;
while (i<=strlen(string)) {
while(j<=300) {
if(string[i] != Alph[j].letter) {
Alph[i].letter = string[i];
Alph[i].times = 1;
}
else {
Alph[j].times++;
}
j++;
}
j=0;
i++;
}
int y,max=0;
char letter_max[0];
for (y=0; y<strlen(string); y++) {
printf("Letter: %c, Times: %d \n", Alph[y].letter, Alph[y].times);
if(Alph[y].times>max) {
max=Alph[y].times;
letter_max[0]=Alph[y].letter;
}
}
printf("\n\n\t\tMost frequent letter: %c - %d times \n\n", letter_max[0], max);
}
I saw you all creating big arrays and "complex" stuff so here I have easy and simple code xD
char most_used_char (char s[]) {
int i; //array's index
int v; //auxiliary index for counting characters
char c_aux; //auxiliary character
int sum = 0; //auxiliary character's occurrence
char c_max; //most used character
int max = 0; //most used character's occurrence
for (i = 0; s[i]; i++) {
c_aux = s[i];
for (v = 0; s[v]; v++)
if (c_aux == s[v]) sum++; /* responsible cycle for counting
character occurrence */
if (sum > max) { //checks if new character is the most used
max = sum;
c_max = c_aux;
}
sum = 0; /* reset counting variable so it can counts new
characters occurrence */
}
return c_max; //this is the most used character!
}

Resources