Reading string over args [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi,
I am trying to read a string into my code via the args[]-parameter, like I would do in Java.
So basically, this is what I want to do:
- read the String "machine" over launch-parameter
- go through every letter of that string in a loop
- while in the loop, check is current letter equals "e"
- if letter equals "e", replace it with "a"
- return edited string
This is the best way to phrase my elemental questions to C. So I'd be happy if you won't take this post offensive.
How could I implement that code?

Here's a solution that (almost) doesn't involve pointers, though you should really learn about pointers if you're going to do even moderately advanced C programming.
void replace_e_with_a(char str[])
{
int i, len = strlen(str);
for (i=0; i<len; i++) {
if (str[i] == e) str[i] = a;
}
}
int main(int argc, char *argv[]){
int i;
for (i = 1; i < argc; i++) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}

Here's something that should work.
#include <stdio.h>
void replace_e_with_a(char * str)
{
int i;
if (NULL != str)
while ('\0' != *str ) {
if (*str == 'e')
*str = 'a';
++str;
}
}
int main(int argc, char **argv){
int i;
for (i = 1; i < argc; ++i) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}

Related

Generate ordered passwords for brute forcing in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I don't know how to explain my need using only words, so let's see an example and check for details.
I want to generate passwords with length of 5 Letters only Upper/Lower case inside my program like this:
aaaaa
aaaab
aaaac
.....
AAAAA
AAAAB
.....
bbbba
bbbbb
.....
Try all combination begining from 'aaaa' until 'ZZZZZ'.
Mixed upper/lower case is allowed and required for my use.
I got this code, but it don't supporte Upper case (I want mixed Lower and Upper case).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isFinish(char *str){
return '\0'== str[strspn(str, "z")];
}
void inc_str(char *str){
int index, carry;
for(index = strlen(str)-1;index>=0;--index){
if(str[index] == 'z'){
carry = 1;
str[index] = 'a';
} else {
carry = 0;
str[index] += 1;
}
if(carry == 0)break;
}
}
int main(){
int n;
char *str;
n=7;//length
str=(char*)malloc(sizeof(char)*(n+1));
//initialize
memset(str, 'a', n);//"aa..aa"
str[n]='\0';
while(1){
printf("%s\n", str);
if(isFinish(str))
break;
inc_str(str);
}
free(str);
return 0;
}
source: https://stackoverflow.com/a/10161603/11622790
I modified this code with the help of #L. Scott Johnson to supprt UPPER/LOWER case.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isFinish(char *str){
return '\0'== str[strspn(str, "Z")];
}
void inc_str(char *str){
int index, carry;
for(index = strlen(str)-1;index>=0;--index){
if(str[index] == 'Z'){
carry = 1;
str[index] = 'a';
} else if(str[index] == 'z'){
carry = 0;
str[index] = 'A';
} else {
carry = 0;
str[index] += 1;
}
if(carry == 0)break;
}
}
int main(){
int n;
char *str;
n=7;//length
str=(char*)malloc(sizeof(char)*(n+1));
//initialize
memset(str, 'a', n);//"aa..aa"
str[n]='\0';
while(1){
printf("%s\n", str);
if(isFinish(str))
break;
inc_str(str);
}
free(str);
return 0;
}

Splitting string sequence into integers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do you split a string:
char *mystring = "12345"
into an integer array which looks like this:
[1, 2, 3, 4, 5]
I have tried something like the code below, but I'm not entirely sure if it's reliable, and I think it will be easy to break. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char *mystring = "12345";
int string_size, i, length;
string_size = strlen(mystring);
int values[string_size];
for (i = 0; mystring[i] != '\0'; i++) {
values[i] = mystring[i] - 48;
}
length = sizeof(values)/sizeof(*values);
for (i = 0; i < length; i++) {
printf("%d ", values[i]);
}
return 0;
}
Which outputs:
1 2 3 4 5
Is there a more C like way I can do this?
The odd thing I see, which isn't itself a problem, is that you calculate the length of the string/array three different ways:
string_size = strlen(mystring);
for (i = 0; mystring[i] != '\0'; i++) {
length = sizeof(values)/sizeof(*values);
where just one method is sufficient:
#include <stdio.h>
#include <string.h>
int main(void) {
char *mystring = "12345";
size_t length = strlen(mystring);
int values[length];
for (int i = 0; i < length; i++) {
values[i] = mystring[i] - '0';
}
for (int i = 0; i < length; i++) {
printf("%d ", values[i]);
}
printf("\n");
return 0;
}
You can replace 48 with '0' for readability.
You can change all loops to loop until string_size like the first one, no need to change the method for each loop.
And finally if you're going to return that array anywhere outside of local function, you should probably malloc() it rather than use a local/stack variable.
But otherwise, it's pretty simple and it works.

C store digit and spaces in char array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can i store digits and spaces in an array? I am using a char array. Here is my code:
char m[100];
int i;
for(i = 0; i < 5; i++)
if(i == 2)
m[i] = ' ';
else
m[i] = i;
How can i print the content of m? (01 34)
Here this should work for you
#include <stdio.h>
int main(void)
{
char m[100];
int i;
for(i = 0; i < 5; i++)
{
if(i == 2)
m[i] = ' ';
else
m[i] = '0' + i; //<< Note ascii of 0 is 48
}
m[i]= '\0';
printf("%s",m);
return 0;
}
Make sure that you set m[i] = '\0' for one-past the element you want to print (this is the null-terminator), then use a printf like function, with %s as the formatter.
For the digits, you need to use m[i] = '0' + i, else you'll be attempting to print control characters rather than the digits themselves.

Alphanumeric String Generation in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone give me a complete example of how to generate an Alphanumeric String randomly
like (ARG534UJ6) using C ? I'm completely new to C.
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
This is not working.
As pointed out by #IngoLeonhardt, use % (sizeof(alphanum) - 1) instead of % sizeof(alphanum)
My guess is that you don't have room for your string, try:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
int main(void)
{
char *str = malloc(8 + 1);
/* initialize random seed: */
srand(time(NULL));
gen_random(str, 8);
printf("%s\n", str);
free(str);
return 0;
}

Why there is an error? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I wrote the following code block I have all the time error in the function find_brackets and calculation. can someone explain to me how to fix it. And the two functions will function together
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void find_brackets(char str[], int len);
void calculation(char str1);
int main(void) {
int len;
char str1[99];
char str[99]; // (4/2)
printf("Enter a math exercises: \n");
gets(str);
len = strlen(str);
find_brackets(str);
calculation(str1);
}
void find_brackets(str[], len) {
char str1[len];
int i, j;
for(i = 0; i < len; i++) {
if(str[i] == '(') {
i++;
while(str[i] != ')') {
str1[j] = str[i];
i++;
j++;
}
}
}
}
void calculation(str1[], len) {
char str[len];
char strp[len];
char str2[len];
char str3[len];
char *rev;
int i, k, j = 0, aPos, zPos;
int sum1, sum2;
float sum;
strcpy (str, str1);
strcpy (strp, str1);
aPos = zPos = -1;
for(i = 0; i < len; i++) {
if(str[i] == '+') {
aPos = i;
}
else if(str[i] == '/') {
zPos = i;
break;
}
}
if(aPos != -1 && zPos != -1) {
for(k = 0, i = zPos-1; i > aPos; --i, ++k) {
str2[k] = str[i];
}
}
rev = strrev(str2);
printf("%s\n", rev);
for(i = 0; i < len; i++) {
if(strp[i] == '/') {
while(strp[i+1] != '+') {
str3[j++] = strp[++i];
}
}
}
printf("%s\n", str2);
sum1 = atoi(str2);
sum2 = atoi(str3);
sum = sum1 / sum2;
printf("%.0f\n", sum);
}
Thanks for the help I appreciate it
Function declaration is void find_brackets(char str[], int len); and the caller from main() is find_brackets(str); which is wrong. Where is the 2nd arg.
Also function calculation() has differnce in declaration and how it is invoked. Maintain a match in function formal arguments followed by callee actual arguments passed.
void find_brackets(str[],len)
void calculation(str1[],len)
please specify data type of len and str[] at function definations.
Also
find_brackets(str);
calculation(str1);
which pass one argument but you declared that with two which is also wrong.

Resources