#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int toggleChars (char* string, char* letters){
int count = 0;
char* scurrent = string;
while(*scurrent != '\0'){
char* lcurrent = letters;
while(*lcurrent != '\0'){
if(*scurrent == *lcurrent){
*scurrent = '0';
count += 1;
}
lcurrent += 1;
}
scurrent += 1;
}
return count;
}
int main(){
char* str = malloc(50);
char* letters = malloc(20);
str = "Hi how R U today?";
letters = "HhiR";
int x = toggleChars(str, letters);
printf("str: %s\ncount: %d", str, x);
return 0;
}
This is just a testing function I made to study for an upcoming exam in C programming. When I compile this and run it, it gives me a segfault. I have deduced with some testing that it is caused by the line
*scurrent = '0';
So it means that I should not change the character at that memory location. But then if I do want to change it, what would I have to do?
There is a problem with your code, the line str = "Hi how R U today?"; will not copy the string to the character array you dynamically allocated, instead it will point to the const char* (read only copy).
So because of this when you are trying to change the contents of str through the toggleChars() function it is throwing a segmentation fault.
You can edit your code as follows:
#include <stdio.h>
int toggleChars (char* string, char* letters){
int count = 0;
char* scurrent = string;
while(*scurrent != '\0'){
char* lcurrent = letters;
while(*lcurrent != '\0'){
if(*scurrent == *lcurrent){
*scurrent = '0';
count += 1;
}
lcurrent += 1;
}
scurrent += 1;
}
return count;
}
int main(){
char str[] = "Hi how R U today?";
char letters[] = "HhiR";
int x = toggleChars(str, letters);
printf("str: %s\ncount: %d", str, x);
return 0;
}
Instead of this, if you want to use dynamic memory allocation you can use strcpy() function from standard library. https://www.geeksforgeeks.org/strcpy-in-c-cpp/
Thanks, hope this helps,
-Rajkumar
Related
Using C, I am trying to implement a function that converts word into mutated_word according to the key string_word. eg: when word is "HE", with the key "QWERTYUIOPASDFGHJKLZXCVBNM", the mutated_word should become "IT". But it keeps giving segmentation fault, not sure how to improve.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void) {
string word = "HE" ;
string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM";
char mutated_word[strlen(word)];
for (int i = 0; word[i] != '\0'; i++) {
string_word[(int)word[i] - 65] = mutated_word[i];
}
printf("%s", mutated_word);
}
You need to terminate the new string with null character.
Your array is too small
Use the correct type for indexes (int is not the correct one)
Check if the character is the letter. If not decide what to do (in my example I convert all letters to uppercase, other chars are left as they are)
Do not use magic numbers. Instead of 65 use 'A'
Your assignment is wrong, you actually want something right opposite.
It will not work with all character encoding.
#include <ctype.h>
#include <stdio.h>
int main (void)
{
string word = "HE" ;
string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM" ;
char mutated_word [strlen(word) + 1];
size_t i;
for (i = 0; word[i] != '\0'; i++)
{
if(isalpha((unsigned char)word[i]))
{
mutated_word[i] = string_word[toupper((unsigned char)word[i]) - 'A'];
}
else
{
mutated_word[i] = word[i];
}
}
mutated_word[i] = 0;
printf("%s", mutated_word);
}
https://godbolt.org/z/4zqq98Y3n
To make it more portable:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
ptrdiff_t findIndex(const char ch, const char * restrict dict)
{
char *result = strchr(dict, ch);
if(!result) return -1;
return result - dict;
}
int main (void)
{
string word = "He124" ;
string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM" ;
string dict = "ABCDEFGHIJKLMNOPQRSTUVXYWZ";
ptrdiff_t index;
char mutated_word [strlen(word) + 1];
size_t i;
for (i = 0; word[i] != '\0'; i++)
{
if(isalpha((unsigned char)word[i]))
{
index = findIndex(toupper((unsigned char)word[i]), dict);
}
else index = -1;
mutated_word[i] = index == -1 ? word[i] : string_word[index];
}
mutated_word[i] = 0;
printf("%s", mutated_word);
}
https://godbolt.org/z/KW8TxxEvq
You program crashes because the assignment is in the wrong order: string_word[(int)word[i] - 65] = mutated_word[i]; is attempting to modify a string literal, which has undefined behavior. Note also that the destination string must be 1 byte longer for the null terminator, which you must set explicitly.
Here is a more portable version:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const char *word = "HE";
const char *normal_word = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *string_word = "QWERTYUIOPASDFGHJKLZXCVBNM";
char mutated_word[strlen(word) + 1];
unsigned char c;
const char *p;
size_t i;
for (i = 0; (c = word[i]) != '\0'; i++) {
if (isupper(c) && (p = strchr(normal_word, c)) != NULL) {
c = string_word[p - normal_word];
} else
if (islower(c) && (p = strchr(normal_word, toupper(c))) != NULL) {
c = string_word[p - normal_word];
c = tolower(c);
}
mutated_word[i] = c;
}
mutated_word[i] = '\0';
printf("%s\n", mutated_word);
return 0;
}
I wanted to split an array to 2 arrays that the first one contains the lowercased letters of the original array and the second one contains the uppercased letters and from some reason it prints some unrelated chars.
#include <stdio.h>
#include <string.h>
#define LEN 8
int main(void)
{
char str[] = "SHaddOW";
char smallStr[LEN], bigStr[LEN];
int i = 0;
int indexSmall = 0;
int indexBig = 0;
for (i = 0; i <= LEN; i++)
{
if (str[i] <= 'Z')
{
smallStr[indexSmall] = str[i];
indexSmall++;
}
if (str[i] >= 'Z')
{
bigStr[indexBig] = str[i];
indexBig++;
}
}
printf("1: ");
puts(smallStr);
printf("2: ");
puts(bigStr);
system("PAUSE");
return 0;
}
Don't define length before you create the string to test.
Create it's length after defining the string to test.
Copy the characters as you encounter them, but as #Ed Heal says you must add a null terminator so that you can print out the two strings (they aren't really strings until they are null terminated).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
char str[] = "SHaddOW";
int len = strlen(str) +1;
char smallStr[len], bigStr[len];
char term[] = {'\0'};
int n, s, b;
s=0;
b=0;
for(n=0; n<len; n++) {
if(islower(str[n])) {
memcpy(smallStr +s, str +n, 1);
s++;
} else if (isupper(str[n])){
memcpy(bigStr +b, str +n, 1);
b++;
}
}
memcpy(smallStr + s, term, 1);
memcpy(bigStr + b , term, 1 );
printf("Upper: %s\n", bigStr);
printf("Lower: %s\n", smallStr);
}
Output:
Upper: SHOW
Lower: add
Add this to the if structure (and other code to support it)
} else {
memcpy(anyStr +a, str +n, 1);
a++;
}
then:
char str[] = ".S1H2a3d4d5O6W.";
and:
printf("Anything else: %s\n", anyStr);
returns:
Upper: SHOW
Lower: add
Anything else: .123456.
A more compact approach with (perhaps) more meaningful variable names:
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
int main ( void ) {
const char str[] = "SHaddOW";
size_t len = strlen(str); /* better to get actual length */
char lowers[len + 1]; /* add one for the nul char */
char uppers[len + 1]; /* (see below) */
int c;
int i = 0;
int n_upper = 0;
int n_lower = 0;
while ((c = str[i++]) != '\0') {
if (isupper(c)) uppers[n_upper++] = c; /* no need to reinvent */
if (islower(c)) lowers[n_lower++] = c; /* the wheel here */
}
uppers[n_upper] = '\0'; /* the nul char ('\0') marks */
lowers[n_lower] = '\0'; /* the end of a C "string" */
printf("1: %s\n", lowers);
printf("2: %s\n", uppers);
return 0;
}
Notes
If you are super concerned about efficiency you could add an else before if (islower...
Adding const means you "promise" the characters in the array won't be changed.
The type size_t is an integer type, but may be larger than int. It is the correct type for the return of strlen(). It is defined in <stdint.h>. None the less, using int will almost always work (on most systems a string would have to be 'yooooge' for its length to be bigger than an int can hold).
The variable c is declared as int instead of char because int is the proper type for the isXXXXX() functions (which are defined in <ctype.h>). It is also a good habit to get into because of the parallels between this loop and another common idiom while ((c = fgetc(fp)) != EOF) ....
You should consider using isupper() and islower() functions. Code would be cleaner. And what if you have some non alpha characters? Your conditions won't work.
for (i = 0; i < LEN; i++)
{
if (islower(str[i]))
{
smallStr[indexSmall] = str[i];
indexSmall++;
}
else if (isupper(str[i]))
{
bigStr[indexBig] = str[i];
indexBig++;
}
}
As #Ed Heal mention. To avoid printing rubbish, after for loopt you should add a null characters to arrays.
smallStr[indexSmall] = '\0';
bigStr[indexBig] = '\0';
Here is my code. I just can't seem to figure it out. Sometimes i get no output, and sometimes i just get 3 random characters, regardless of how long the entered string is.
#include <stdio.h>
#include <math.h>
#include <string.h>
void reverse(char* array, int numberOfChars);
int main()
{
char string[250];
int length;
printf("Enter a string: ");
gets(string);
printf("How long is the string:");
scanf("%d", &length);
reverse(string, length);
printf("Reversed string is: %s\n"), string;
return 0;
}
void reverse(char *userArray, int numberOfChars)
{
char temp;
int fromEnd = 0, fromStart = 0;
fromEnd = numberOfChars;
while (fromStart < fromEnd)
{
temp = userArray[fromStart];
userArray[fromStart] = userArray[fromEnd];
userArray[fromEnd] = temp;
fromStart++;
fromEnd--;
}
}
I really dread asking these questions here but I can't seem to fix it...
Any help appreciated
Conceptually you need to swap the ends until you are left with a string of length 0 or 1. You don't need to test for the length of the remaining portion of the string after each iteration however, because it can be shown that exactly length/2 swaps will be needed.
void reverse (char *s)
{
size_t length = strlen (s);
for (size_t i = 0; i < length / 2; i++) {
char tmp;
tmp = s[i];
s[i] = s[length - 1 - i];
s[length - 1 - i] = tmp;
}
}
There is no need to complicate things like that, try this:
#include <stdio.h>
int main(void){
char *src = "Michi";
char dest[256];
int i=-1,j=0;
while(src[++i]!='\0');
while(i>=0){
dest[j++] = src[--i];
}
dest[j]='\0';
printf("Your new string is: %s",dest);
return 0;
}
Output:
Your new string is: ihciM
This is probably what you need:
#include<stdio.h>
#include<string.h>
void reverse(char *ptr);
int main(void) {
char src[256] = "Michi";
reverse(src);
printf("Your new string is: %s",src);
return (0);
}
void reverse(char *src){
char dest;
size_t i, j = 0;
i = 0;
j = strlen(src) - 1;
while (i < j) {
dest = src[i];
src[i] = src[j];
src[j] = dest;
i++;
j--;
}
}
don't use gets(),use fgets().To reverse strings,you don't need to pass number of characters,since strings in C are null-terminated.check this very simple function:
#include <stdio.h>
void reverse(char *_Str);
int main(void)
{
char str[] = "Hello Buddy";
reverse(str);
printf("%s\n",str);
return 0;
}
void reverse(char *_Str)
{
char tmp,*_b,*_e;
_b = _e = _Str;
while(*_e) _e++;
_e--;
while(_b < _e)
{
tmp = *_b;
*_b++ = *_e;
*_e-- = tmp;
}
}
Well the very important line you missed is assigning the null character. And do not take the length of your string as an input from the user. use the function some_integer=strlen(stringname); This will return the length of your stringname and assign it to some_integer. Your function to reverse the string should be as
void reverse(char *userarray) // no need of a second argument. Do not trust your users.
{
char temp;
int fromEnd,fromStart = 0; /
fromEnd = strlen(userarray)-1; // here the length of your string is assigned to fromEnd.
while (fromStart < fromEnd)
{
temp = userArray[fromStart];
userArray[fromStart] = userArray[fromEnd];
userArray[fromEnd] = temp;
fromStart++;
fromEnd--;
}
userarray[strlen(userarray)-1]='\0'; //You missed this line (very important)
}
And chek your printf statement.
It should be
printf("Your reversed string is %s \n",string);
not
printf("Your reversed string is %s \n"),string;
I belive this will work. Check it and let me know if it works for you.
Incorrect code. The needed string for the printf() is not in the function. #M Oehm
/// printf("Reversed string is: %s\n"), string;
printf("Reversed string is: %s\n", string);
Also original code can easily wipe out the string terminating null character '\0'. Better to use strlen(string) rather than ask the user for the length.
Likely should use - 1 as commented by #WalterM. It is unclear what values OP is using.
// fromEnd = numberOfChars;
if (numberOfChars <= 0) return;
fromEnd = numberOfChars - 1;
Answers I have seen so far depend on int well addressing all elements of a string. size_t is the right approach as int may be too small.
Many answers would fail on a string such as "".
So here is another contribution without those restrictions.
#include <string.h>
#include <stdio.h>
char *str_revese_inplace(char *s) {
char *left = s;
char *right = s + strlen(s);
while (right > left) {
right--;
char t = *right;
*right = *left;
*left = t;
left++;
}
return s;
}
void stest(const char *s) {
char t[strlen(s) + 1];
// or char t[100];
strcpy(t, s);
printf("'%s' --> '%s'\n", s, str_revese_inplace(t));
}
int main(void) {
stest("123");
stest("12");
stest("1");
stest("");
return 0;
}
Output
'123' --> '321'
'12' --> '21'
'1' --> '1'
'' --> ''
segmentation fault after running the following code:
int main(){
const char *string = "This is not reverse";
char *reverse;
char temp;
int length, i = 0;
length = strlen(string);
strcpy(reverse, string);
while(i < (length/2)){
temp = *(reverse+i); // this is the reason for segmentation fault..but why?
i++;
}
}
could someone please explain the reason for the same
You need to allocate space for reverse. For example
char *reverse = malloc(strlen(string) + 1);
By the way, the rest of the code seems to have an algorithmic bug. I think what you wanted to do was something like:
#include <string.h>
#include <malloc.h>
int main(){
const char *string = "This is not reverse";
int length = strlen(string);
char *reverse = malloc(length + 1);
int i = 0;
while(i < length){
*(reverse + i) = *(string + length - i - 1);
i++;
}
printf("%s\n", reverse);
return 0;
}
instead of strcpy(reverse, string); use reverse = string, Because you don't need to allocate space for reverse, Just point reverse to string i.e reverse = string
I've got a block of strings, say "aaa\0bbbb\0ccccccc\0"
and I want to turn them into an array of strings.
I've tried to do so using the following code:
void parsePath(char* pathString){
char *pathS = malloc(strlen(pathString));
strcpy(pathS, pathString);
printf(1,"33333\n");
pathCount = 0;
int i,charIndex;
printf(1,"44444\n");
for(i=0; i<strlen(pathString) ; i++){
if(pathS[i]=='\0')
{
char* ith = malloc(charIndex);
strcpy(ith,pathS+i-charIndex);
printf(1,"parsed string %s\n",ith);
exportPathList[pathCount] = ith;
pathCount++;
charIndex=0;
}
else{
charIndex++;
}
}
return;
}
exportPathList is a global variable defined earlier in the code by
char* exportPathList[32];
when using that function exportPathList[i] contains garbage.
What am I doing wrong?
The answer to this SO question:
Parse string into argv/argc
deals with a similar issue, you might have a look.
You need to know how many strings are there or agree for an "end of strings". The simplest would be to have an empty string at the end:
aaa\0bbbb\0ccccccc\0\0
^^
P.S. is this homework?
First of all, since your strings are delimited by a null char, '\0', strlen will only report the size of the string up to the first '\0'. strcpy will copy until the first null character as well.
Further, you cannot know where the input string ends with this information. You either need to pass in the whole size or, for example, end the input with double null characters:
#include <stdio.h>
#include <string.h>
void parsePath(const char* pathString){
char buf[256]; // some limit
while (1) {
strcpy(buf, pathString);
pathString+=strlen(buf) + 1;
if (strlen(buf) == 0)
break;
printf("%s\n", buf);
}
}
int main()
{
const char *str = "aaa\0bbbb\0ccccccc\0\0";
parsePath(str);
return 0;
}
And you need some realloc's to actually create the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 16
char* exportPathList[MAXSIZE] = {0};
size_t pathCount = 0;
void parsePath(char* pathString){
char *ptop, *pend;
ptop=pend=pathString;
while(*ptop){
while(*pend)++pend;
exportPathList[pathCount++]=strdup(ptop);
pend=ptop=pend+1;
}
}
int main(){
char textBlock[]= "aaa\0bbbb\0ccccccc\0";
//size_t size = sizeof(textBlock)/sizeof(char);
int i;
parsePath(textBlock);
for(i=0;i<pathCount;++i)
printf("%s\n", exportPathList[i]);
return 0;
}
The solution I've implemented was indeed adding double '\0' at the end of the string and using that in order to calculate the number of strings.
My new implementation (paths is the number of strings):
void parsePath(char* pathString,int paths){
int i=0;
while (i<paths) {
exportPathList[i] = malloc(strlen(pathString)+1);
strcpy(exportPathList[i], pathString);
pathString+=strlen(pathString);
i++;
}
}
I'd like to thank everyone that contributed.
My Implementation looks like this -> it follows the idea of argv and argc in a main funtion:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char **args = (char**)malloc(100*sizeof(char));
char buff[100], input_string[100], letter;
for(int i = 0; i < 100; i++){
buff[i] = '\0';
input_string[i] = '\0';
}
for(int i = 0; (letter = getchar())!='\n'; i++){
input_string[i] = letter;
}
int args_num = 0;
for(int i = 0, j = 0; i < 100;i++){
if((input_string[i] == ' ')||(input_string[i]=='\0')){
//reset j = 0
j = 0;
args[args_num] = malloc(strlen(buff+1));
strcpy(args[args_num++],buff);
for(int i = 0; i < 100; i++)buff[i] = '\0';
}else buff[j++] = input_string[i];
}
for(int i = 0; i < args_num; i++){
printf("%s ",args[i]);
}
}
-> Every single word in your string can then be accessed with args[i]