I'm wondering why my duplicate check is giving me wrong output. The idea is to built a nested loop, that compares every following letter in an array, with the initial loop's one. However, if I print the results, the function gives back true when A = K e.g. and I don't understand that behaviour. Anyone able to explain what's happening here?
for (int n = 0; n < strlen(argv[1]) ; n++)
{
for (int i = 0; i < strlen(argv[1]) ; i++)
{
if (argv[1][n] == argv[1][i + 1])
{
printf("argv[1][n] = %c\n", argv[1][n]);
printf("argv[1][i] = %c\n", argv[1][i]);
printf("Error.\n");
return 0;
}
}
}
A more efficient way to check for duplicate chars in a string. Only requires one for-loop instead of a nested pair of loops. Assumes an 8-bit char - hence 256 as array size.
size_t table[256] = {0};
size_t positions[256] = {0};
const char* sz = argv[1];
const size_t len = strlen(argv[1]);
for (size_t i = 0; i < len; i++)
{
unsigned char index = (unsigned char)(sz[i]);
table[index]++;
if (table[index] > 1)
{
printf("duplicate char %c found at index %d. Originally seen at index %d\n", sz[i], i, (int)(positions[index]));
return 0;
}
else
{
positions[index] = i;
}
}
These for loops
for (int n = 0; n < strlen(argv[1]) ; n++)
{
for (int i = 0; i < strlen(argv[1]) ; i++)
{
if (argv[1][n] == argv[1][i + 1])
{
//...
}
}
}
do not make a sense because argv[1][n] can be the same letter at the same position as argv[1][i+1] because the inner loop starts from 0.
Also you are outputting a letter at position i
printf("argv[1][i] = %c\n", argv[1][i]);
but in the preceding if statement you are checking a letter at the position i + 1.
The loops can look the following way
for ( size_t i = 0, n = strlen( argv[1] ); i < n ; i++ )
{
for ( size_t j = i + 1; j < n; j++ )
{
if ( argv[1][i] == argv[1][j] )
{
printf( "argv[1][i] = %c\n", argv[1][i]);
printf( "argv[1][j] = %c\n", argv[1][j]);
printf("Error.\n");
return 0;
}
}
}
Instead of the inner loop you could use the standard C function strchr. Here is a demonstrative program.
#include <stdio.h>
#include <string.h>
int unique( const char *s )
{
while ( *s && !strchr( s + 1, *s ) ) ++s;
return *s == '\0';
}
int main(void)
{
char *s = "12345";
printf( "\"%s\" -> %d\n", s, unique( s ) );
s = "12341";
printf( "\"%s\" -> %d\n", s, unique( s ) );
return 0;
}
The program output is
"12345" -> 1
"12341" -> 0
You may call the function passing as an argument the command line argument argv[1]. For example
If ( !unique( argv[1] ) ) puts( "Error." );
Why A = K is because you are printing the i-th index printf("argv[1][i] = %c\n", argv[1][i]); when you are checking i+1th index if (argv[1][n] == argv[1][i + 1]). You are printing the wrong character that is checked with your if statement.
Also, be careful on that i+1 and your loop condition.
Related
I need to make a program that will print characters in a word on how frequent it is used. The unique characters will be printed in increasing order (spaces are ignored), if there are ties the character with lower ascii value will be printed first.
For an example if the input is hello world, the letters "h", "e", "w", "r" and "d" are only used once, the character "o" is used twice and the character "l" is used thrice. Since h,e,w,r,d are tie we should sort it into d,e,h,r,w. Then next would be o since it is used twice and then last is l. Thus if the input is hello world the output must be dehrwol. On my current program the problem is that when there are ties, it would not sort it alphabetically so the output is hewrdol instead of dehrwol.
This is the code I have written
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int times[256];
int cmpLetters(const void* a, const void* b)
{
return (times[*(char*)a] > times[*(char*)b]) - (times[*(char*)a] < times[*(char*)b]);
}
int main()
{
char letters[256];
int i, j, k, lnum, t;
char s[1000];
fgets(s, sizeof(s), stdin);
// Init occurrences as 0
memset(times, 0, sizeof(times));
for (i = lnum = 0; s[i] != '\0'; i++)
if (times[s[i]]++ == 0)
letters[lnum++] = s[i];
// Sort letters by number of occurrences
qsort(letters, lnum, sizeof(char), cmpLetters);
char* new = malloc(sizeof(char) * (i + 1));
for (j = k = 0; j < lnum; j++)
for (i = 0; i < times[letters[j]]; i++)
new[k++] = letters[j];
// new[k] = '\0';
for (i = 0; i<lnum; i++)
{
if(letters[i] != '\n' && letters[i] !=' ')
printf("%c",letters[i]);
}
printf("\n\n");
return 0;
}
In this for loop
for (i = lnum = 0; s[i] != '\0'; i++)
if (times[s[i]]++ == 0)
letters[lnum++] = s[i];
you are not checking whether s[i] represents a letter.
The comparison function
int cmpLetters(const void* a, const void* b)
{
return (times[*(char*)a] > times[*(char*)b]) - (times[*(char*)a] < times[*(char*)b]);
}
compares only characters without comparing also their frequencies.
This code snippet
char* new = malloc(sizeof(char) * (i + 1));
for (j = k = 0; j < lnum; j++)
for (i = 0; i < times[letters[j]]; i++)
new[k++] = letters[j];
does not make sense because the array new is not used further in the program. It only produces a memory leak.
The program will be simpler if to introduce a structure that contains two data members that store a letter and its frequency.
Here is a demonstration program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Pair
{
char c;
size_t n;
};
int cmp( const void *a, const void *b )
{
const struct Pair *p1 = a;
const struct Pair *p2 = b;
int result = ( p2->n < p1->n ) - ( p1->n < p2->n );
if (result == 0)
{
result = ( p2->c < p1->c ) - ( p1->c < p2->c );
}
return result;
}
int main( void )
{
enum { N = 1000} ;
char s[N];
fgets( s, sizeof( s ), stdin );
size_t n = 0;
for (size_t i = 0; s[i] != '\0'; ++i)
{
if (isalpha( ( unsigned char )s[i] ))
{
size_t j = 0;
while (j != i && s[j] != s[i]) ++j;
n += j == i;
}
}
if (n != 0)
{
struct Pair pairs[n];
memset( pairs, 0, n * sizeof( struct Pair ) );
for (size_t i = 0, m = 0; s[i] != '\0'; i++)
{
if (isalpha( ( unsigned char )s[i] ))
{
size_t j = 0;
while (j != m && pairs[j].c != s[i]) ++j;
if (j == m)
{
pairs[m].c = s[i];
++pairs[m].n;
++m;
}
else
{
++pairs[j].n;
}
}
}
qsort( pairs, n, sizeof( *pairs ), cmp );
for (size_t i = 0; i < n; i++)
{
putchar( pairs[i].c );
}
putchar( '\n' );
}
}
The program output might look like
hello world
dehrwol
I'm asked to check whether a given string can be a palindrome after rearranging and then return true if it can be a palindrome or false if it cannot be a palindrome.
I'm getting a runtime error: Segmentation fault while running tests.
Here's my code:
bool palindromeRearranging(char * inputString) {
int index;
int count, count1 = 0;
for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{ count = 0;
if(inputString[i] == '*')
{
continue;
}else
{
for(int j = i+1; j < b ;j++)
{
if(inputString[i] == inputString[j] )
{
inputString[j] = '*';
count++;
index = i;
}
}
inputString[index] = '*';
}
if(count %2 == 0 && count != 0)
{
count1++;
}
}
for(int i = 0, b = strlen(inputString); i < b; i++)
{
if(inputString[i] != '*')
{
count1++;
}
}
if(count1 > 1)
{
return false;
}else
{
return true;
}
}
Here inputString is the string given. I tried to replace the two characters with * if they are both same. And then counting the no. of single characters.
5 out of 10 test cases have passed.
For example test cases like "abbcabb", "aabb", "zyyzzzzz" have passed.
But I'm getting runtime error: Segmentation fault for test cases like "abcad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa". "abca", "abdhuierf".
I hope you could help me with this one.
Btw I'm solving this on codesignal.com, under the arcade section/intro. Q.18 palindromeRearranging.
As it was already mentioned you are using the non-initialized variable index.
int index;
in this for loop
for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{ count = 0;
if(inputString[i] == '*')
{
continue;
}else
{
for(int j = i+1; j < b ;j++)
{
if(inputString[i] == inputString[j] )
{
inputString[j] = '*';
count++;
index = i;
}
}
inputString[index] = '*';
}
if in the inner for loop of the else statement a repeated character was not found that results in undefined behavior.
Or you can use an invalid value of the variable index that it keeps after a previous iteration of the loop.
Also not all characters that satisfy the condition
if(inputString[i] == inputString[j] )
are substituted for the character '*'.
But in any case your approach is invalid. You shall not change the original string. Otherwise after calling your function the caller will deal with a modified string.
The function can be written for example the following way as it is shown in the demonstrative program below.
#include <stdio.h>
int can_be_palindrome( const char *s )
{
size_t odd = 0;
for ( const char *p = s; odd < 2 && *p; ++p )
{
const char *q = s;
while ( q != p && *q != *p ) ++q;
if ( q == p )
{
size_t n = 1;
while ( *++q )
{
if ( *q == *p ) ++n;
}
odd += n % 2;
}
}
return odd < 2;
}
int main(void)
{
const char *s = "abbcabb";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "aabb";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "zyyzzzzz";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "abca";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
return 0;
}
The program output is
"abbcabb" can be a palindrome is true
"aabb" can be a palindrome is true
"zyyzzzzz" can be a palindrome is true
"abca" can be a palindrome is false
"Why am I getting “runtime error: Segmentation fault” for random test cases?"
Aside from your function not being logically viable for testing palindromes, it will fail on random occasions due to undefined behavior:
int index;
Is not initialized,
It fails here for example when its value exceeds the array index:
inputString[index] = '*';
With the following sequence of run-time messages:
To address this, change this:
int index;
int count, count1 = 0;
to this:
int index = 0;
int count = 0, count1 = 0;
Aside, here is an example of a similar question and answer.
Error is due to unintialized behaviour of your int index;
I have an array of char pointers (string array), which contains some duplicate values. I've found an algorithm that truncates the array by removing its duplicate values.
Here is a code sample :
int i, j , k;
int n = 10;
char *teams[n];
for(i=0;i<n;i++){
for(j=i+1;j<n;){
if(*(team[j]==*(team[i])){
for(k=j;k<n;k++){
//strcpy(team[k], team[k+1]);
team[k] = team[k+1];
}
n--;
}else{
j++;
}
}
}
I've read that the only way to copy strings between string arrays is to use strcpy(s1, s2). But in my case I can't use it, because strcpy function permits to copy s2 into s1 only if s2 has a lenght equal or bigger than the lenght of s1. So how can I implement this algorithm if I can't put the string pointed by the pointer team[k+1] in team[k] ?
It seems you need to remove duplicated string representations instead of duplicated addresses to strings.
If so then this if statement (if to add missed closed parenthesis)
if( *(team[j] ) ==*( team[i] ) ){
compares only first characters of strings instead of comparing strings pointed to by the pointers.
In this loop
for(k=j;k<n;k++){
//strcpy(team[k], team[k+1]);
team[k] = team[k+1];
}
each time when a duplicates string is found there is copied the whole array of pointers. Moreover there is an attempt to access memory beyond the array in this statement when k is equal to n-1
team[k] = team[k+1];
^^^^
You can write a separate function that will "remove" duplicates. The function can for example return pointer after the last unique element in the modified array.
#include <stdio.h>
#include <string.h>
char ** unique( char *s[], size_t n )
{
size_t i = 0;
for ( size_t j = 0; j < n; j++ )
{
size_t k = 0;
while ( k < i && strcmp( s[k], s[j] ) != 0 ) ++k;
if ( k == i )
{
if ( i != j ) s[i] = s[j];
++i;
}
}
return s + i;
}
int main(void)
{
char * s[] = { "A", "B", "A", "C", "A" };
const size_t N = sizeof( s ) / sizeof( *s );
for ( size_t i = 0; i < N; i++ ) printf( "%s ", s[i] );
printf( "\n" );
char **p = unique( s, N );
size_t n = p - s;
for ( size_t i = 0; i < n; i++ ) printf( "%s ", s[i] );
printf( "\n" );
return 0;
}
The program output is
A B A C A
A B C
#include <stdio.h>
#include <string.h>
unsigned dedup(char **arr, unsigned count)
{
unsigned this, that ;
for(this=0;this<count;this++){
for(that=this+1;that<count;){
if( strcmp(arr[that], arr[this])) {that++; continue; }
#if PRESERVE_ORDER
memmove(arr+that, arr+that+1, (--count - that) * sizeof arr[that] );
#else
arr[that] = arr[--count];
#endif
}
}
return count; /* the count after deduplication */
}
char *array[] = { "one", "two", "three", "two", "one", "four", "five", "two" };
int main(void)
{
unsigned count, index;
count = dedup(array, 8);
for (index = 0; index < count; index++) {
printf("%s\n", array[index] );
}
return 0;
}
[UPDATED]: I added the PRESERVE_ORDER version
I have created a function that should find the numerical position of the first character of a substring in a larger string. I am having some problems with the output and I am not too sure why. These problems include -1 being returned every single time instead of the integer position of the substring. I have debugged and cannot trace where the function goes wrong.
This is how the function should perform: If my string is "The dog was fast" and I am searching for the substring "dog", the function should return 4. Thanks to chqrlie for help with the loop.
Here is the function:
int findSubString(char original[], char toFind[]) {
size_t i, j;
int originalLength = 0;
int toFindLength = 0;
originalLength = strlen(original) + 1;
toFindLength = strlen(toFind) + 1;
for (i = 0; i < toFindLength + 1; i++) {
for (j = 0; j < originalLength + 1; j++) {
if (toFind[j] == '\0') {
return i;
}
if (original[i + j] != toFind[j]) {
break;
}
}
if (original[i] == '\0') {
return -1;
}
}
}
The function parameters cannot be modified, this is a requirement. Any help appreciated!
These statements inside the loops
if (toFind[j] == '\0') {
return i;
}
results in undefined behavior because the string toFind can be shorter than the string original.
The same is valid for this loop
if (original[i + j] != toFind[j]) {
break;
}
because i + j can be greater than the length of the string original.
And there is no need to scan all characters of the string original if you are going to find a substring inside it.
Also you should check whether the length of the string original is not less than the length of the string toFind.
If you want to find only the first character of the string toFind in the string original it is enough to use standard C function strchr. If you want to find the whole string toFind in the string original then you could use another C standard function strstr.
If you want to write the function yourself to find a string in other string then it can look for example the following way
I declared the function like
long long int findSubString( const char original[], const char toFind[] );
however you can write its declaration as you like for example like
int findSubString( char original[], char toFind[] );
But in this case you should declare function local variable success like
int success = -1;
and output the result using format specifier "%d" instead of "%lld".
Here you are.
#include <stdio.h>
#include <string.h>
#include <stddef.h>
long long int findSubString( const char original[], const char toFind[] )
{
size_t n = strlen( original );
size_t m = strlen( toFind );
long long int success = -1;
if ( !( n < m ) )
{
n = n - m + 1;
for ( size_t i = 0; success == -1 && i < n; i++ )
{
size_t j = 0;
while ( j < m && original[i+j] == toFind[j] ) j++;
if ( j == m ) success = i;
}
}
return success;
}
int main(void)
{
printf( "%lld\n", findSubString( "The dog was fast", "dog" ) );
return 0;
}
Its output is
4
Your loops are reversed. The outer loop should walk positions from zero to originalLength, inclusive; the nested loop should walk positions from zero to toFindLength, inclusive.
Both originalLength and toFindLength should be set to values returned by strlen, not strlen plus one, because null terminator position is not a good start.
Finally, you are returning -1 from inside the outer loop. This is too early - you should be returning -1 only after you are done with the outer loop as well.
Your loop counter tests are incorrect: wrong upper limit and the limits are off by one. Note that the tests are actually not necessary as you exit both loops when hitting the '\0' terminators.
Here is a simpler version:
int findSubString(const char *original, const char *toFind) {
for (size_t i = 0;; i++) {
for (size_t j = 0;; j++) {
if (toFind[j] == '\0') {
return i;
}
if (original[i + j] != toFind[j]) {
break;
}
}
if (original[i] == '\0') {
return -1;
}
}
}
There is a small advantage at computing the string lengths to reduce the number of comparisons in pathological cases such as findSubString("aaaaaaaaaaa", "aaaaaaaaaaaa");
int findSubString(const char *original, const char *toFind) {
size_t originalLength = strlen(original);
size_t toFindLength = strlen(toFind);
if (toFindLength <= originalLength) {
for (size_t i = 0; i <= originalLength - toFindLength; i++) {
for (size_t j = 0;; j++) {
if (toFind[j] == '\0') {
return i;
}
if (original[i + j] != toFind[j]) {
break;
}
}
}
}
return -1;
}
I program in C. I'm supposed to create a program which identifies what is the most common character in a string and what's the second most common character.
I'm not sure why, but it's not working. the program should put into an integer the location of it. Not a pointer but if the most common is str1[i] then it will put into an integer the value of i. And so in the second most common. If it is str1[j] than it should put into an integer the value of j. then, it's supposed to replace the most commons with the second most common. The replacement function works, there's probably a problem in the loops although I can't figure out what is it.
Here's what I have (assume all of the integers and strings are declared in the beginning):
void stringReplace(char str1[], char ch1, char ch2);
int main()
{
char str1[100];
char ch1, ch2;
int i, j, p, n, len, counter1, counter2, first, second, times;
printf("Please enter the string - maximum = 100 characters:\n");
printf("User input: ");
gets(str1);
len = strlen(str1);
for(i = 0 ; i < len ; i++)
{
counter1 = 0;
for(j = 0 ; j < len ; j++)
{
if(str1[i] == str1[j])
{
counter1++;
}
if(counter1 > counter2)
{
first = i;
}
}
counter2 = counter1;
} //character which shows up most - found.
counter2 = 0;
for(p = 0 ; p < len ; p++)
{
for(n = 0 ; n < len ; n++)
{
if(str1[p] == str1[n])
{
counter1++;
}
if(counter1 < first && counter1 > counter2)
{
second = p;
}
}
counter2 = counter1;
}
ch1 = str1[first];
ch2 = str1[second];
stringReplace(str1, ch1, ch2);
puts(str1);
return 0;
}
void stringReplace(char str1[], char ch1, char ch2)
{
int i, j, len;
len = strlen(str1);
for(i = 0 ; i <= len ; i++)
{
if(str1[i] == ch1)
{
str1[i] = ch2;
}
}
}
Where's the problem ?
So you want to find the n max numbers in a string, being n=2, right?
I did a little working example for you. The code differs slightly from yours but it works well.
#include <stdio.h>
int main(int argc, char* argv[])
{
char str[] = "Algorithms Are Funnnn!\0";
int i=0;
int offset=33;
int ocurrs[94] = {0}; //considering from 33 to 126 (valid chars - ASCII Table [0-127]).
int max[2]={0};
while(str[i])
ocurrs[str[i++]-offset]++;
for(i=0; i<94; i++)
if(ocurrs[i]>ocurrs[max[1]]){
max[0] = max[1];
max[1] = i;
}
else if(ocurrs[i]>ocurrs[max[0]])
max[0]=i;
printf("chars '%c'(%d times) and '%c'(%d times) occurred most.\n",
offset+max[1], ocurrs[max[1]], offset+max[0], ocurrs[max[0]]);
return 0;
}
Also, try to stay away from gets as it's totally unsafe.
If you want to grab 100 chars max, use this instead:
char buffer[100];
fgets(buffer, 100, stdin);
Regards.
Can't resist to find an answer with some loops, without the populate occurence technique.
Very fun to code.
#include <string.h>
#include <stdio.h>
int main( void ) {
char szInput[] = "ONE DOES NOT SIMPLY WALK INTO MORDOR!";
int len = strlen( szInput );
int MaxCountSoFar_1 = 0;
int MaxIndexSoFar_1 = -1;
int MaxCountSoFar_2 = 0;
int MaxIndexSoFar_2 = -1;
int i, j, CountThatOne;
for ( i = 0; i < len; ++i ) {
if ( szInput[ i ] == ' ' ) continue;
// count that char
CountThatOne = 1;
// don't start from 0, they are already "counted"
for ( j = i + 1; j < len; ++j ) {
if ( szInput[ i ] == szInput[ j ] ) ++CountThatOne;
}
if ( CountThatOne > MaxCountSoFar_1 ) {
// push old first max to new second max
MaxCountSoFar_2 = MaxCountSoFar_1;
MaxIndexSoFar_2 = MaxIndexSoFar_1;
// new first max
MaxCountSoFar_1 = CountThatOne;
MaxIndexSoFar_1 = i;
} else {
// catch second one, but not if equal to first
if ( CountThatOne > MaxCountSoFar_2 && szInput[ i ] != szInput[ MaxIndexSoFar_1 ] ) {
MaxCountSoFar_2 = CountThatOne;
MaxIndexSoFar_2 = i;
}
}
}
if ( MaxIndexSoFar_1 >= 0 ) {
printf( "Most seen char is %c, first seen at index %d\n", szInput[ MaxIndexSoFar_1 ], MaxIndexSoFar_1 );
if ( MaxIndexSoFar_2 >= 0 ) {
printf( "Second Most seen char is %c, first seen at index %d\n", szInput[ MaxIndexSoFar_2 ], MaxIndexSoFar_2 );
}
}
return 0;
}