#include <stdio.h>
void main()
{
char plaintext[50];
char key[20];
int plain=0,max1=0,max2=0; // max2 amount of coloumn n max1 for row on chip
char chip[30][30];
int i,j,k=0,c=0;
printf("Enter key :");
scanf("%s",&key);
for(i=0;key[i]!='\0';i++)
{
max2++;
}
printf("Enter plaintext :");
scanf("%s",&plaintext);
for( i = 0; plaintext[i] != '\0'; i++ )
{
plain++;
}
if (plain%max2==0)
max1=plain/max2;
else
max1=plain/max2+1;
while(plaintext[k]!='\0')
{
for (i=0;i<max1;i++)
{
for (j=0;j<max2;j++)
{
chip[i][j]=plaintext[k];
k++;
}
}
}
printf("%s",chip[0][0]);
}
1st im trying to move the string on plain (1D array) into chip(2d array) with dynamic array on the 2D but when im trying to run this code it show nothing than stopped working.. is there anything goes wrong with my 2D array?
I compiled it on Linux (after omitting the void because in Linux main has to return a value) and I got "Segmentation fault". I guess it is because of printf("%s",chip[0][0]);, it should be printf("%s",chip[0]);
I mean chip[0][0] returns a specific character in the chip[0] and you want to print the first string in chip array, right?
Related
i having problem scaning strings into a matrix that each string is maximum 256 long.
it tried this but it didnt work, someone have a solution?
#include<stdio.h>
#include<string.h>
void main()
{
char* song[5][256];
for (int i = 0; i < 5; i++)
scanf("%255[^\n]s", song[i]);
}
welcome to stackoverflow,
first of all i am not sure why you are using char *song[5][256],but you can achieve the required out by using a 2-dimentional array ie..char song[5][256].and also i am sure that using scanf function for a matrix like array will always cause a trouble.
#include<stdio.h>
int main()
{
char s[5][256];
for(int i=0;i<5;i++)
{
for(int j=0;j<256;j++)
{ char c=getchar();
if(c=='\n'||c== EOF)
{
s[i][j]='\0';
printf("\n");
break;
}
if(j<255)
s[i][j]=c;
if(j==255)
s[i][j]='\0';
}
printf("the given string %d is:%s \n",i+1,s[i]);
}
return 0;
}
And also i have used two for loops, for assigning the elements of that array.Now the code works fine.I used s insted of your variale name song.
Hope that I answered your question.
#include <stdio.h>
#include <string.h>
int main(void)
{
char alpha[26] = { '0' };
char nl;
while (alpha != '0'){
scanf("%c", &alpha);
scanf("%c", &nl);
printf("the character is %c\n", alpha);
}
int i, j, size;
for (i=0;i<size;i++){
for (j=i;j<size;j++){
if (alpha[i]<alpha[j]){
Swap(&alpha[i], &alpha[j]);
}
}
}
printf("%s", alpha);
return 0;
}
I'm getting an error "comparison between pointer and integer" in my while loop. I'm wanting to read in each letter of the alphabet from a text file and stop when it reaches a "0" at the end of the list. It's then going to sort alphabetically starting with z,y,x.. etc. How else could I write this so it stops at "0" without using an integer?
Thanks for the help
alpha [26] is an array of 26 chars, with your while loop you always overwrite the first element of the array (scanf("%c", &alpha); overwrites the first element of alpha in every iteration of the loop ) , the entire code will not work. to access the elements of the array you can either use pointers or indexes, indexes are easier, try a for loop
int i;
for (i = 0; i<26 ; i++)
{
if(alpha[i] != '0')
{
scanf("%c", &alpha[i]);
printf("the character is %c\n", alpha[i]);
}
}
C: scanf to array
for using pointers to access array see Can i use pointer in scanf to take input in an array?
the objective of my question is very simple. The first input that I get from the user is n (number of test cases). For each test case, the program will scan a string input from the user. And each of these strings I will process separately.
The question here is how can I get string inputs and process them separately in C language??? The idea is similar to the dictionary concept where we can have many words which are individual arrays inside one big array.
The program I have written so far:
#include <stdio.h>
#define max 100
int main (){
int n; // number of testcases
char str [100];
scanf ("%d\n",&n);
for (int i =0;i <n;i++){
scanf ("%s",&str [i]);
}
getchar ();
return 0;
}
Can someone suggest what should be done?
The input should be something like this:
Input 1:
3
Shoe
Horse
House
Input 2:
2
Flower
Bee
here 3 and 2 are the values of n, the number of test cases.
First of all, Don't be confused between "string" in C++ , and "Character Array" in C.
Since your question is based on C language, I will be answering according to that...
#include <stdio.h>
int main (){
int n; // number of testcases
char str [100][100] ; // many words , as individual arrays inside one big array
scanf ("%d\n",&n);
for (int i =0;i <n;i++){
scanf ("%s",str[i]); // since you are taking string , not character
}
// Now if you want to access i'th word you can do like
for(int i = 0 ; i < n; i++)
printf("%s\n" , str[i]);
getchar ();
return 0;
}
Now here instead of using a two-dimensional array, you can also use a one-dimensional array and separate two words by spaces, and store each word's starting position in some another array. (which is lot of implementation).
First of all yours is not C program, as you can't declare variable inside FOR loop in C, secondly have created a prototype using Pointer to Pointer, storing character array in matrix style datastructure, here is the code :-
#include <stdio.h>
#include <stdlib.h>
#define max 100
int main (){
int n,i; // number of testcases
char str [100];
char **strArray;
scanf ("%d",&n);
strArray = (char **) malloc(n);
for (i =0;i <n;i++){
(strArray)[i] = (char *) malloc(sizeof(char)*100);
scanf ("%s",(strArray)[i]);
}
for (i =0;i <n;i++){
printf("%s\n",(strArray)[i]);
free((strArray)[i]);
}
getchar ();
return 0;
}
#include <stdio.h>
#define MAX 100 // poorly named
int n=0; // number of testcases
char** strs=0;
void releaseMemory() // don't forget to release memory when done
{
int counter; // a better name
if (strs != 0)
{
for (counter=0; counter<n; counter++)
{
if (strs[counter] != 0)
free(strs[counter]);
}
free(strs);
}
}
int main ()
{
int counter; // a better name
scanf("%d\n",&n);
strs = (char**) calloc(n,sizeof(char*));
if (strs == 0)
{
printf("outer allocation failed!")
return -1;
}
for (counter=0; counter<n; counter++)
{
strs[counter] = (char*) malloc(MAX*sizeof(char));
if (strs[counter] == 0)
{
printf("allocate buffer %d failed!",counter)
releaseMemory();
return -1;
}
scanf("%s",&strs[counter]); // better hope the input is less than MAX!!
// N.B. - this doesn't limit input to one word, use validation to handle that
}
getchar();
// do whatever you need to with the data
releaseMemory();
return 0;
}
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;
}
I have a problem in C where i have to find number of occurrence of each character in a string.Suppose i have string like "amitamt" and output should be like "a2m2it2" .I have a routine from which i can find no of occurrence of a particular character.
int count_chars(const char* string, char ch)
{
int count = 0;
int i;
int length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
But I am not sure how could I count each character of string
If you have an ASCII string, create an int array of size 256. Then loop through the string and increment the value in the int array on position x. While x is the ASCII value of the character in your string you're looping through.
if i have any mistakes like syntax please excuse as im working on vb , Im unable to figure out where to put braces or brackets ,
and I belive strchr makes your task easier
#include <stdio.h>
#include <string.h>
int str_occ (char *pch ,char a)
{
int i = 0;
char *p;
p=strchr(pch,a);
while (p!=NULL)
{
i = i+1;
p = strchr(p+1,a);
}
return i;
}
To explain the code *pch is the string you have to pass ,char a is the alphabet you are searching to find how many times its occurring and int i returns the value of number of occurrences
say sample
int main()
{
char a[]="hello world";
int i;
i=str_occ(a,'l');
printf("%d",i);
}
output is 3
You can make the code as per your requirements, keep caling the function inside a loop , I mean rotate your elements