In C programme:
Question is: You are given a binary string S of size N. Now you need to tell total how many 01 and 10 pair exist in the given string.
#include<stdio.h>
int main() {
int i,t,j,count;
int n;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&n);
char s[n];
scanf("%s",&s[n]);
j=0,count=0;
while(s[j]!='\0')
{
if(s[j]=='1' && s[j+1]=='0')
{ count++;}
else if(s[j]=='0' && s[j+1]=='1')
{ count++;}
j++;
}
printf("%d\n",count);
}
return 0;
}
When n is less than 6,there is showing correct answer. But when n is greater then 6, this is showing 0. Please help me out.
scanf("%s",&s[n]); - that &s[n] is the address where s ends. It's the first address after s. You want to write to the first address of s: &s[0] or just s.
Related
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);
}
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;
}
given a number, i was aked to find the next number which is a palindrome.Thisis the code i have writen. my code works fine but the website I am working on says "time limit exceeded"...how do I correct this?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
void reverse (char s[]){
int c,i,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--){
c= s[i];
s[i]=s[j];
s[j]=c;
}
}
void itoa (int n, char s[]) {
int i;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i] = '\0';
reverse(s);
}
int main (void) {
int t,j;
scanf("%d",&t);
for(j=0;j<t;j++){
int k,c=1,i;
char s[MAXLEN];
scanf("%d",&k);
int a= k+1;
while (c!=0){
itoa(a,s);
int e=strlen(s)-1;
for(i=0;i<(e+1)/2;i++){
if (s[i]==s[e-i]){
c=0;
}
else{
c=1;
goto state;
}
}
state:a++;
}
printf("%s\n",s);
}
return 0;
}
You need to use a smarter way than brute force!
Here is an algorithm with time complexity log(N):
Read the number as a string.
Take the first half of the number.
Make a palindrome by adding the first half reversed. (Consider the two cases: even/odd length of the original number)
If this palindrome is greater than the original number you are done.
If it is not:
Take the first half of the number and add 1.
Make a palindrome by adding the first half reversed. (Again consider the two cases: even/odd length of the original number).
Done!
I wrote this small program to count the number of trailing zeroes. I got my algorithm correct. But I cannot get the output right. The first line we enter is for the number of inputs (T). Later the user enters the number (whose number of trailing zeroes in factorial is to be calculated.) And then print the answer (count). But after I input the value for N, I get the answer on the third line (I used just one '\n'). I need to get my output right.
#include <stdio.h>
int main()
{
int T;
int i,j,temp,count=0;
long int N;
scanf("%d",&T);
for(i=0;i<T;i++)
{
scanf("\n%ld",&N);
for(j=5;j<=N;j+=5)
{
temp=j;
while(j > 1)
{
if(j%5 == 0)
count++;
j=j/5;
}
j=temp;
}
printf("\n%d",count);
count =0;
}
return 0;
}
Change:
printf("\n%d",count);
to:
printf("%d\n",count);
and:
scanf("\n%ld",&N);
to:
scanf("%ld",&N);
This program takes an input of number of strings followed by the actual strings. The output should be the number of common characters to all strings.
The constraints are:
No of strings <= 100
Length of string <= 100
For example..
Input:
3
abc
bcd
cde
Output:
1
As only c is common to all strings.
It gives right output when used with small inputs.
But when used with large strings like this :https://hr-testcases.s3.amazonaws.com/2223/input19.txt?AWSAccessKeyId=AKIAINGOTNJCTGAUP7NA&Expires=1408959130&Signature=E%2BMnR6MA0gQNkuWHMvc70eCL5Dw%3D&response-content-type=text%2Fplain
It gives wrong output of 58 instead of 19.
This is my code :
#include<stdio.h>
#include<string.h>
void main(){
int n,i,j,count=0;
char s[100][100];
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",s[i]);
}
int t;
int l = strlen(s[0]);
for(i=0;i<l;i++){
t=0;
for(j=1;j<n;j++){
if(strchr(s[j],s[0][i])!='\0'){
t++;
}
}
if(t==n-1)
count++;
}
printf("%d",count);
}
As you iterate over the first string's characters, you might potentially find the same character more than one time.
This means that common chars found more than one time in the first string will be counted more than one time.
This is what causing your program to calculate 58 instead of 19.
Check below some quick update to your program - it treats the duplicates in the first string.
This program calculates 19 on your 100 strings' test case.
#include<stdio.h>
#include<string.h>
void main(){
int n,i,j/*,count=0*/;
int count[26] = {0}; /* counter per char */
char s[100][101];
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",s[i]);
}
int t;
int l = strlen(s[0]);
for(i=0;i<l;i++){
t=0;
/* convert char to integer - assuming lowercase char only */
int char_index = s[0][i] - 'a';
for(j=1;j<n;j++){
if(strchr(s[j],s[0][i])!='\0' && count[char_index] == 0){
t++;
}
}
if(t==n-1)
count[char_index] = 1;
/* count++; */
}
/* count how many chars are 1*/
int count_n = 0;
int index;
for (index = 0; index < 26; index ++)
{
if (count[index] == 1)
count_n ++;
}
printf("\n\n%d",count_n);
}