Change letter in array for 3 different letter - c

I need to change the text in array in a way, that everywhere I have a character, I need to change it to123.
Example: for the given text: ayasxka I should got this: 12123k123 or this 12323k123 text.
I almost got it to work, but instead of k between numbers, I got s, I mean, this is my result: 12123s123.
int main()
{
int i, j = 0;
char t[] = "ayasxka";
char *r = malloc(sizeof(char) * (strlen(t) + 2));
memset(r, '\0', (strlen(t) + 1));
for(i=0; t[i] != '\0'; i++)
{
if(t[i] == 'a')
{
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else
r[i+2] = t[i];
}
printf("%s\n", r);
free(r);
return 0;
}

In your algorithm you should use:
for(i=0; t[i] != '\0'; i++)
{
if(t[i] == 'a')
{
j = i;
r[j++] = '1';
r[j++] = '2';
r[j++] = '3';
}
else if(j==i)
r[j++] = t[i];
}
r[j] = '\0';
and in your malloc you should add +1 character for the '\0', because strlen() doesn't count that, so
char *r = malloc(3 + strlen(t));
instead of
char *r = malloc(2 + strlen(t));
This will give you 12123k123

The problem is strlen(t) + 2 is not enough, consider the worst case i.e. the string is just made of only a characters, then it should be
char *r = malloc(3 * strlen(t) + 1);
and szieof(char) == 1 is madatory by the way.
And you will need a counter for the position in the r string, say j
Try this
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
int i, j = 0;
char t[] = "ayasxka";
/* don't call strlen multiple times, store the value if it wont change */
size_t length = strlen(t);
/* it doesn't matter how unlikely malloc will fail, check that + */
char *r = malloc(3 * length + 1); /* | */
if (r == NULL) /* <--------------------------------------------+ */
return -1;
for (i = 0 ; t[i] != '\0' ; i++)
{
if(t[i] == 'a')
{
r[j++] = '1';
r[j++] = '2';
r[j++] = '3';
}
else
r[j++] = t[i];
}
/* you don't need the memset */
r[j] = '\0';
printf("%s\n", r);
free(r);
return 0;
}

int i;// j = 0;//unused `j`
char t[] = "ayasxka";
char *r = calloc(strlen(t) + 2 + 1, sizeof(char));//change size, +2: for last a, +1: for NUL
//memset(r, '\0', (strlen(t) + 1));//calloc initialize by 0
for(i=0; t[i] != '\0'; i++){
if(t[i] == 'a'){
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else if(r[i] == '\0'){//Not yet been set to the value
r[i] = t[i];
}
}
printf("%s\n", r);//12123k123
free(r);
char t[] = "ayasxka";
int i, len = strlen(t);
char *r = calloc(len + 2 + 1, sizeof(char));
for(i=len-1; i>=0; --i){
if(t[i] == 'a'){
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else if(r[i] == '\0'){
r[i] = t[i];
}
}
printf("%s\n", r);//12323k123
free(r);

Related

Converting a string into 2d array in c

hi i have been trying to convert a string map to an 2d array but the more i change the more i get confused. now it does not compile and complains its = NUL (edited i added malloc and seems that and gives segmentation fault).
#include <unistd.h>
#include <stdlib.h>
char **stoa(char *str)
{
int i;
int j;
int k;
char **map;
//int len = ft_strlen_line(str);
//int row = ft_strlen(str);
i = 0;
j = 0;
k = 0;
//arow = row/len;
//map = malloc(j * k sizeof(char*));
map = malloc(j * sizeof(k));
while(str[i])
{
if (str[i] == '\n')
{
k = 0;
j++;
}
map[j][k] = str[i];
k++;
i++;
}
free(map);
return (map);
if (!(map = malloc(j * k * sizeof(char))))
return NULL;
}
int main(void)
{
char row12[] = "ABABABABAB\nABABABABAB\nABABABABAB\n\0";
char **map;
map = stoa(row12);
return (0);
}
Not tested.
char **stoa(char *str)
size_t nlines = 0;
size_t pos = 0;
char **array = NULL;
char **tmp;
while(*str)
{
while(*str && str[pos++] != '\n');
nlines++;
tmp = realloc(array, (nlines + 1) * sizeof(*array));
if(tmp) array = tmp;
else break;
array[nlines] = NULL;
if(array[nlines - 1] = malloc(pos + 2));
if(array[nlines - 1])
{
memcpy(array[nlines - 1], str, pos);
array[nlines - 1][pos] = 0;
str = str + pos;
if(str == '\n') str++;
pos = 0;
}
else break;
}
return array;
}

Segmentation fault in HackerRank C program

This is the challenge I'm trying to solve:
https://www.hackerrank.com/challenges/querying-the-document/
So far I have made progress but I am stuck at a Segmentation Fault and I can't figure out why.
The following is an abridged version of the whole source code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char**** get_document(char* text) {
int spaces = 0, periods = 0, newlines = 0;
for(int i = 0; i != strlen(text); i++) {
if(text[i] == ' ')
spaces++;
else if(text[i] == '.')
periods++;
else if(text[i] == '\n')
newlines++;
}
char** para[periods + 1]; // each paragraph stores that many sentences
char* senten[spaces + 1]; // each sentence stores that many words
char*** doc[newlines + 1]; // each document stores that many paragraphs
int start = 0, k = 0, m, p = 0, x = 0;
for(int i = 0; i != strlen(text); i++) {
if(text[i] == ' ' || text[i] == '.') { // space or period means there was a word before it
senten[k] = (char*) malloc(sizeof(char) * (i - start)); // store each word
m = 0;
for(int j = start; j < i; )
senten[k][m++] = text[j++];
senten[k][m++] = '\0'; // append a '\0' to end the string
if(text[i + 1] == '\n') { // newline means that a new paragraph is starting
para[p] = senten; // store pointer to sentence in para
while(k)
senten[k--] = NULL; // don't need now
start = i + 2;
p++;
}
else
start = i + 1;
k++;
}
if(i == strlen(text) - 1) { // end of file
doc[x++] = para; // store pointer to paragraph in doc
while(p)
para[p--] = NULL; // don't need
}
}
return doc;
}
int main()
{
char* text = "Hello World.\nHi.Bye.";
char**** doc = get_document(text);
printf("%s",doc[0][0][0]); // should print "Hello"
return 0;
}
In short the program is supposed to take a char* and output a char****.
Example:
If char * text = "Hello World.\nHi.Bye.";
Then char **** doc = {{{"Hello","World"}},{{"Hi"},{"Bye"}}};
You return the local variable in get_document function:
char** para[periods + 1]; // each paragraph stores that many sentences
char* senten[spaces + 1];
char*** doc[newlines + 1];
...
return doc;
It's bad idea, Because out of get_document function, the variable doc maybe does not exist.
You should use:
char*** para = malloc((periods + 1) * sizeof(char **));
char** senten = malloc((spaces + 1) * sizeof(char *));
char**** doc = malloc((newlines + 1)*sizeof(char ***));
This is how I modified my code. Now it passes all the test cases.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char**** get_document(char* newtext) {
int spaces = 0, periods = 0, newlines = 0;
for(int i = 0; i != strlen(newtext); i++) {
if(newtext[i] == ' ')
spaces++;
else if(newtext[i] == '.')
periods++;
else if(newtext[i] == '\n')
newlines++;
}
char*** para = malloc((periods + 1) * sizeof(char **));
char** senten = malloc((spaces + 1) * sizeof(char *));
char**** doc = malloc((newlines + 1) * sizeof(char ***));
int start = 0, k = 0, m, p = 0, x = 0, f = 0, pp = 0;
int pcount = 0;
for(int i = 0; i != strlen(newtext); i++) {
if(newtext[i] == '.') pcount++;
if(newtext[i] == ' ' || newtext[i] == '.') { // space or period means there was a word before it
senten[k] = (char*) malloc(sizeof(char) * (i - start) + 1); // store each word
m = 0;
for(int j = start; j < i; )
senten[k][m++] = newtext[j++];
senten[k][m++] = '\0'; // append a '\0' to end the string
k++;
if(i != strlen(newtext) && newtext[i + 1] == '\n') // newline means that a new paragraph is starting
start = i + 2;
else
start = i + 1;
if(i != strlen(newtext) && isalpha(newtext[i + 1]) && newtext[i] == '.') {
para[p++] = senten;
senten = malloc((spaces + 1) * sizeof(char *));
k = 0;
}
if((i != strlen(newtext) && newtext[i + 1] == '\n') || i + 1 == strlen(newtext)) {
para[p++] = senten;
senten = malloc((spaces + 1) * sizeof(char *));
k = 0;
doc[f++] = &(para[pp]);
pp += pcount;
pcount = 0;
}
}
}
return doc;
}
int main()
{
char* newtext = "Hello World.\nHi.Bye.\nWow.";
char**** doc = get_document(newtext);
printf("%s\n", doc[0][0][0]);
printf("%s\n", doc[0][0][1]);
printf("%s\n", doc[1][0][0]);
printf("%s\n", doc[1][1][0]);
printf("%s\n", doc[2][0][0]);
return 0;
}

Given two strings, find the words that are common to both the strings

Eg: input: char *str1 = "the are all is well";
char *str2 = "is who the";
output: common words in two given strings, return 2D array of strings.
#define SIZE 31
char ** commonWords(char *str1, char *str2) {
int i,j=0,count1,count2,k=0,a,b,m=0,n;
char str3[100][100], str4[100][100];
char **output;
output = (char **)malloc(SIZE*sizeof(char*));
if (str1 == NULL || str2 == NULL)
{
return NULL;
}
for (i = 0; str1[i] != '\0'; i++)
{
if (str1[i] != ' ')
{
str3[j][k++] = str1[i];
}
else
{
str3[j][k++] = '\0';
j++;
k = 0;
}
}
str3[j][k++] = '\0';
count1 = j > 0 ? j + 1 : j;
j = k = 0;
for (i = 0; str2[i] != '\0'; i++)
{
if (str2[i] != ' ')
{
str4[j][k++] = str2[i];
}
else
{
str4[j][k++] = '\0';
j++;
k = 0;
}
}
str4[j][k++] = '\0';
count2 = j > 0 ? j + 1 : j;
for (i = 0; i < count1; i++)
{
for (j = 0; j < count2; j++)
{
if (str3[i][k] == str4[j][k])
{
if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2] == '\0')
{
a = i;
b = k;
while (str3[a][b] != '\0')
{
output = (char **)malloc(SIZE*sizeof(char));
output[m][n] = str3[a][b];
n++;
b++;
}
output[m][n] = '\0';
}
else if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2])
{
a = i;
b = k;
while (str3[a][b] != '\0')
{
output = (char **)malloc(SIZE*sizeof(char));
output[m][n] = str3[a][b];
n++;
b++;
}
output[m][n] = '\0';
m++;
}
}
}
}
return output;
}
I am debugging this code in visual studios and the test is failed.Its showing this " message: Exception code: C0000005" .It means error related to memory space allocation.So where did i go wrong?
You have the statement
output = (char **)malloc(SIZE*sizeof(char));
at two lines of your program.
You have to modify this statement in order allocate memory for the double pointer output of type char**, but you also need to allocate memory for every element of output like this :
int i;
output = (char **)malloc(SIZE*sizeof(char*));
for (i = 0; i < SIZE; i++)
output[i] = (char *)malloc(x*sizeof(char));
where x is the desired size.
Also check for NULL pointer return, for instance
if (output[i] == NULL)
....

Big number adding function crashes program

I wrote an adding function for very large numbers and when it gets called, the program crashes. I am assuming that it has to do with the carrying. Here is the code:
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX];
char usea = strrev(a);
char useb = strrev(b);
char ret[strlen(useadd)];
char *pa, *pb, *padd;
padd = &useadd;
pa = &usea;
pb = &useb;
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){
if(temp + atoi(*pa) + atoi(*pb) < 20){
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10;
}
else{
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20;
}
}
else{
*padd = atoi(*pa) + atoi(*pb);
temp = 0;
}
++padd;
++pa;
++pb;
}
i = 0;
while(useadd[i] != '\0'){
ret[i] = useadd[i];
++i;
}
return strrev(ret);
}
Thanks for all of the help. I'm sorry if this ends up being a stupid mistake.
Your program has so many problems!
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX]; // MAX might not be large enough
char usea = strrev(a); // should not modify argument a
// strrev() is not standard C undefined on my system
// if defined, it returns char*, not char
char useb = strrev(b); // see above
char ret[strlen(useadd)]; // useadd is uninitialized -> undefined behaviour
char *pa, *pb, *padd;
padd = &useadd; // & is incorrect, useadd is an array
pa = &usea; // same as above
pb = &useb; // idem
// forgot to initialize temp to 0
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){ // atoi converts a string, not a char
if(temp + atoi(*pa) + atoi(*pb) < 20){ // same... sum cannot exceed 19
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10; // atoi...
}
else{ // never reached
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20; // atoi...
}
}
else{
*padd = atoi(*pa) + atoi(*pb); // same atoi problem
temp = 0;
}
++padd;
++pa;
++pb;
}
// if a and b have a different size, loop fails to copy digits and propagate carry
// if temp is not 0, you forget to add the leading '1'
// trailing '\0' is not set
i = 0;
while(useadd[i] != '\0'){ // undefined behaviour, '\0' not set.
ret[i] = useadd[i];
++i;
}
// forgot to set the final '\0'
// why not use strcpy?
return strrev(ret); // strrev not defined.
// if defined, returning ret, address of local array
}
Here is a complete rewrite:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *bigadd(const char a[], const char b[]) {
int ia = strlen(a);
int ib = strlen(b);
int size = 2 + (ia > ib ? ia : ib), ic = size - 1, temp = 0;
char *res = malloc(size);
if (res) {
res[--ic] = '\0';
while (ia + ib) {
if (ia > 0) temp += a[--ia] - '0';
if (ib > 0) temp += b[--ib] - '0';
res[--ic] = '0' + temp % 10;
temp /= 10;
}
if (temp) {
memmove(res + 1, res, size - 1);
*res = '1';
}
}
return res;
}
int main(int argc, char *argv[]) {
for (int i = 1; i + 1 < argc; i += 2) {
char *res = bigadd(argv[i], argv[i+1]);
printf("%s + %s = %s\n", argv[i], argv[i+1], res);
free(res);
}
}

Trying to compare two strings

It is sufficient to say that I am new to C so please have show some mercy ;).
I'm trying to compare two strings. The output shouldn't contain common characters. Sadly it does.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char msg[15] = "blueberry";
int c;
int s[15];
int j = 0;
int i = 0;
int k= 0;
int ok = 0;
int t = 0;
while (i < 15 && (c = getchar()) != '\n')
{
s[i] = c;
++i;
}
for (t=j=0; t < 15; ++t)
{
ok = 1;
//printf ("%c", s[t]);
}
for (k=0; msg[k] != '\0'; ++k)
{
if (s[t] == msg[k])
{
ok = 0;
}
}
if (ok == 1)
{
s[j] = s[t];
j++;
}
s[j] = '\0';
for (j = 0; j < 15; ++j)
printf ("%c ", s[j]);
}
The input from the keyboard is blackberry, expected output should've been U but sadly it is not. Any help please. Also why is it entering the nested for loop irrespective of condition?
My big thanks to everyone, it helped me a lot. I've figured out a way & am ok with the output. I've borrowed some ideas from A4L :).
To compare two string, you can use strcmp().
The following is a string compare program that you can use for your reference. I has both array and pointer version for better understanding.
#include <stdio.h>
int strcmp1(char a[], char b[])
{
int i=0;
while (a[i] == b[i]) {
if (a[i] == '\0')
return 0;
i++;
}
return a[i]-b[i];
}
int strcmp2(char *a, char *b)
{
while (*a == *b) {
if (*a == '\0')
return 0;
a++; b++;
}
return *a-*b;
}
int main()
{
char s1[] = "test string1";
char s2[] = "test string";
char s3[] = "aaa";
char s4[] = "bbb";
printf("strcmp1(%s, %s) = %d \n", s1, s2, strcmp1(s1, s2));
printf("strcmp2(%s, %s) = %d \n", s3, s4, strcmp2(s3, s4));
return 0;
}
given that msg contains "blueberry" and s contains "blackberry" this should do it
for (int i=0; i < strlen(msg); i++) {
for (int j = 0; j < strlen(s); j++) {
if (msg[i] != s[j]) {
printf ("%c", msg[i]);
}
}
}
yes it's ugly (using the strlen in the for gives me the chills, but I'm still low on coffeine today ^^)
i guess you want to find the first letter where the input differs from message
here is your own code with some fixes
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char msg[15] = "blueberry";
int c;
char s[15];
int i = 0;
int k= 0;
int ok = 0;
while (i < 15 && (c = getchar()) != '\n')
{
s[i] = (char) c;
++i;
}
// make sure to terminate the string after hitting enter
s[i] = '\0';
printf("input: %s\n", s);
printf("messg: %s\n", msg);
// run through both input and message with one counter
for (k=0; ok == 0 && msg[k] != '\0' && s[k] != '\0'; )
{
// if different chars stop
if (s[k] != msg[k]){
ok = 1;
} else {
// next char
k++;
}
}
if (ok == 1)
{
printf ("diff # index %d -> %c\n", k, msg[k]);
}
else
{
printf ("no diff\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
//Length to match
int comm(char* s1, char* s2){
int len = 0;
while(*s1 && *s2 && *s1++ == *s2++)
++len;
return len;
}
//commdiffcomm
/*
int commr(char* s1, char* s2){
int len = 0, limit;
int len1,len2;
len1 = strlen(s1);
len2 = strlen(s2);
limit = len1 > len2 ? len2 : len1;
s1 = s1 + len1;
s2 = s2 + len2;
while(limit-- && *--s1 == *--s2)
++len;
return len;
}
//bad
int diff(char* s1, char* s2, int* len1, int* len2){
int len, lenr, s1_len, s2_len, wk_max, i, j;
len = comm(s1, s2);
if(strcmp(s1, s2)==0){
*len1 = *len2 = 0;
return len;
}
lenr = commr(s1, s2);
*len1 = strlen(s1) - len - lenr;
*len2 = strlen(s2) - len - lenr;
return len;
}
*/
int diff(char* s1, char* s2, int* len1, int* len2){
int len, s1_len, s2_len, wk_max, i, j;
len = comm(s1, s2);
if(strcmp(s1, s2)==0){
*len1 = *len2 = 0;
return len;
}
s1_len = strlen(s1 + len + 1);
s2_len = strlen(s2 + len);
wk_max = 0;
for(i = 1; i < s1_len ; i++){
for(j = 0; j < s2_len; j++){
int com_len;
com_len = comm(s1 + len + i, s2 + len + j);
if(wk_max < com_len){
wk_max = com_len;
*len1 = i;
*len2 = j;
}
}
}
return len;
}
int main(){
char str1[16] = "blueberry";
char str2[16] = "blackberry";
char dif1[16] = "";
char dif2[16] = "";
int len0;//length of top to diff pos
int len1;
int len2;
len0 = diff(str1, str2, &len1, &len2);
strncpy(dif1, str1 + len0, len1);
strncpy(dif2, str2 + len0, len2);
if(len1 !=0 && len2 != 0){
printf("%s different %s at position %d length %d (\"%s\")\n", str1, str2, len0, len1, dif1);
printf("%s different %s at position %d length %d (\"%s\")\n", str2, str1, len0, len2, dif2);
} else {
printf("two string is same.");
}
return 0;
}
/*
blueberry different blackberry at position 2 length 2 ("ue")
blackberry different blueberry at position 2 length 3 ("ack")
*/
There are a few problems with the code as is:
You don't null-terminate your input string. Attempting to use it with c string functons would spell trouble. To fix that, change
while (i < 15 && (c = getchar()) != '\n')
{
s[i] = c;
++i;
}
to
while (i < 14 && (c = getchar()) != '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
Your specification is unclear as to whether you want your program to print the letters unique to msg, or to both s and msg. (i.e, do you want msg-s or (msg ∪ s)-(msg ∩ s) Assuming the first, the important part of your program goes like this:
k=0;
for(i=0;i<strlen(msg);i++){
int exists = 0;
for(j=0;!exists && j<strlen(s);j++){
if(msg[j] == s[i])
exists = 1;
}
if(!exists)
msg[k++] = msg[i];
}
s[k] = '\0';
The inner loop checks if s contains the current character in msg. If it does, we don't do anything, but if it doesn't, we append it to the end of a sublist we're creating on top of the bits of msg we've already processed.
your code is a mess even after the rewrite - there are too many errors to describe in detail
/*
blackbery
b l u e b e r r y
. . a c k b e . .
result = non-equal
*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char msg[15] = "blueberry";
int c, s[15], i,j,k, ok;
for (i=0; i < 15; i++) s[i] = 0;
for (i=0; i < 15 && (c = getchar()) != '\n'; i++) s[i] = c;
for (ok=1, k=0; msg[k] != '\0'; ++k)
if (s[k] != msg[k]) ok = 0; else s[k] = '.';
for (j = 0; j < 15; ++j) printf ("%c ", msg[j]);
printf("\n");
for (j = 0; j < 15; ++j) printf ("%c ", s[j]);
printf("\nresult = %s\n", ok ? "equal" : "non-equal");
}

Resources