Randomize letters in string in C - c

I have for example "asd" and I want it to be randomized to DAS, DSA, SAD, you know. How can I code this? Tried a few solutions but It didnt really work.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main()
{
printf("type in the word\n");
char haslo[128];
scanf("%s", haslo);
char set[128];
char hasloa[128];
strcpy(set, haslo);
unsigned int Ind = 0;
srand(time(NULL) + rand());
int len = strlen(set);
while(Ind < len)
{
hasloa[Ind++] = set[rand()%62];
}
hasloa[len] = '\0';
printf("%s", hasloa);
return 0;
}

Change 62 inside the while loop to "len"

Related

I am trying to input a string and make the first letter uppercase

I don't get an error but when I do run the program it repeats what I stated
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define strsize 25
void cap(char *str_cap);
int main()
{
char str[strsize];
printf("enter a word with less than 25 letters\n---->");
gets(str);
cap(str);
I tried using toupper in printf but that did not work either
also I am not sure how to isolate the first letter
printf("%s",str,1);
return 0;
}
void cap(char *str_cap)
{
if(islower(str_cap) || isupper(str_cap))
toupper (str_cap);
(str_cap,1);
}
It is good if functions return value.
You ask for the first letter not first char in the string which makes a difference when you want to convert " hello".
Check for the error conditions.
char *cap(char *str_cap)
{
char *saved = str_cap;
if(*str_cap)
{
while(*str_cap && !isalpha((unsigned char)*str_cap))
{
str_cap++;
}
if(*str_cap) *str_cap = toupper((unsigned char)*str_cap);
}
return saved;
}
Just calling toupper() is not going to change the string's first character to uppercase. You have to set the first character of the string explicitly.
Have a look at the following implementation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define strsize 25
void cap(char *str_cap);
int main()
{
char str[strsize];
printf("enter a word with less than 25 letters\n---->");
fgets(str, strsize, stdin);
cap(str);
printf("%s",str);
return 0;
}
void cap(char* str_cap)
{
int length = strlen(str_cap);
for(size_t i=0;i<length;i++){
if(isalpha(str_cap[i])){
if(islower(str_cap[i])){
str_cap[i] = toupper(str_cap[i]);
}
break;
}
}
}
Input:
andy baker
Output:
---->Andy baker

Storing user-inputted strings into a string array in c?

Fairly new to C, I am trying to read a file of multiple words using bash indirection, and put the words into a string array. The end of the file is marked with a -1.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[400000];
init(words);
int i = 0;
do{
printf("%s",words[i]);
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int i = 0;
do{
fgets(words[i],1024,stdin);
i++;
}while(!strcmp(words[i],"-1"));
}
This gives me a segmentation fault, if any other information is needed I'm more than happy to provide it.
If I guessed correctly, '400000' means the max lines the user can input. But the default size of stack on Windows OS is 1M, sizeof(void*) * 400000 = 1,600,000...
The other thing is that you have not allocated memory for every line.
So, I try to correct your code like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 4000 // '400000' is really too big!
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[MAX_LINE];
memset(words, 0 , sizeof(words));
init(words);
int i = 0;
do{
printf("%s",words[i]);
delete words[i];
words[i] = nullptr;
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int maxLen = 1024;
int i = 0;
do{
words[i] = new char[maxLen];
memset(words[i], 0, maxLen);
fgets(words[i], maxLen, stdin);
i++;
}while(!strcmp(words[i],"-1") && i < MAX_LINE);
}

Error While Using String LIbrary Function

This is the error that I am facing.
ERROR: warning: passing argument 1 of ‘__builtin_strlen’ makes pointer from integer without a cast [-Wint-conversion]
Question Link:Hackerrank Balanced Brackets
My Code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
char rev(char c){
char ch;
if(c=='[')
ch=']';
else if(c=='{')
ch='}';
else if(c=='(')
ch=')';
return ch;
}
int main(){
int t; //test cases
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
char* s = (char *)malloc(10240 * sizeof(char));
int l,ret;
scanf("%s",s);
l=strlen(s);
char *st;
st=(char *)malloc(l * sizeof(char));
int top=-1;
for(int i=0;i<l;i++){
char ch=s[i];
char chr=rev(ch);
ret=strcmp(st[top],chr);
if(ret!=0){
top++;
st[top]=ch;
}
else{
top=top-1;
}
}
if(top==-1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
I shall be really thankful if anybody can help me out.
Thanks in advance.

What is wrong with my program reversing strings in C?

I want to create a function to reverse a string in C. I found a couple pre-made on the internet but I wish to create mine. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille*sizeof(char));
for(j=taille-1, i=0; j>0; j--, i++){
*(inverse+i)=*(chaine-j);
}
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s", inv);
return 0;
}
I can't figure out why I get a segmentation fault.
There were several errors in your code, the most significant being the offset from chaine in the wrong direction. Also, lack of space for a string terminator, and j ending prematurely.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* inverseCarac(char* chaine){
if(chaine==0||*chaine==0) return NULL;
int j, i;
int taille=strlen(chaine);
char* inverse=malloc(taille+1); // add 1 for terminator
for(j=taille-1, i=0; j>=0; j--, i++){ // change j>0 to j >= 0
*(inverse+i)=*(chaine+j); // change -j to +j
}
inverse[taille] = '\0'; // write terminator
return inverse;
}
int main(void){
char* test="bonjour";
char* inv=inverseCarac(test);
printf("%s\n", inv);
return 0;
}
Program output
ruojnob

Debugger shows wrong value to pointer

I have the following code:
#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <ostream>
#include <sstream>
#include <bitset>
#include <numeric>
#include<fstream>
using namespace std;
char str[] = "this.is.a.test";
char str2[] = "this.is.another.test";
typedef struct
{
size_t count;
char** strings;
} Tokens;
Tokens Tokenize(char* String, char Split)
{
Tokens t;
t.count = 1;
for (size_t i = 0; String[i] != 0; i++)
{
if (String[i] == Split)
t.count++;
}
t.strings =(char**) malloc(sizeof(char*)* t.count);
if (t.count > 0)
t.strings[0] = String;
for (size_t i = 0, j = 1; String[i] != 0; i++)
{
if (String[i] == Split)
{
t.strings[j] = &String[i + 1];
String[i] = 0;
j++;
}
}
return t;
}
int main(void)
{
Tokens t = Tokenize(str, '.');
printf("number of strings: %i\n---\n", t.count);
for (size_t i = 0; i < t.count; i++)
{
printf("%i: %s\n", i, t.strings[i]);
}
free(t.strings);
}
The problem is when I debug the code and especially that line t.strings[j] = &String[i + 1];
In a test case of this.is.a.test
At the first found dot . , it should points to this, but in the debugger it shows the following picture.
enter code here
What the debugger shows is correct at line 55. The assignment has been made, so t.strings[j] points after the dot.
Note that in Tokenize you allocate Tokens t; on the stack and later return this t. That is bad (very bad!). Because t is on the stack, it will be overwritten by the call to printf.
(And although most is C, formally it is C++ as in C you cannot declare a variable in the for initialization, as in for (size_t i = 0;)

Resources