I'm trying to assign the elements of string str[] to rev[] using this code below, but it keeps giving me the error:
"string subscript out of range" error.
How do I correct this?
for (int i = 0; i <= str.length(); i++)
{
for (int j = str.length(); j >= 0; j--)
{
rev[i] = str[j];
}
}
For an array index range from 0 to array size - 1.
You don't need two loops to reverse an array.
In java you can do reversal like this:
int length = str.length - 1;
for (int i = 0; i <= str.length-1; i++) {
rev[length - i] = str[i];
}
Related
The module that swaps row to make an array sorted in ascii ascending order keeps returning error.
const int MAX = 10;
const int MAX_STR = 80;
void asciiOrder(char (*buffer)[MAX_STR]);
void asciiOrder(char (*buffer)[MAX_STR]) {
char * temp;
for (int i = 0; i < sizeof(buffer) / sizeof (buffer[0]) - 1; i++) {
for (int j = 1; i + j < sizeof(buffer) / sizeof(buffer[0]); j++) {
for (int k = 0; k < strlen(buffer[i]) && buffer[i][k] != NULL; k++) {
if (buffer[i][k] > buffer[i+j][k]) {
temp = buffer[i+j];
buffer[i+j] = buffer[i];
buffer[i] = temp;
break;
}
}
}
}
}
And this is the error I got from the entire code:
practice102.c:87:23: error: assignment to expression with array type
buffer[i+j] = buffer[i];
^
practice102.c:88:21: error: assignment to expression with array type
buffer[i] = temp;
I searched other posts but still don't know why.
How to solve this problem?
C forbidden to assign a 2D array without specify the two index.
You have to write something like
buffer[a][b] = buffer[c][d]
whatever your a,b,c,d are.
Also be sure that your buffer temp are filled and/or allocated.
I am trying to make array of strings each representing one card of a poker deck from 2 strings (ranks, colors). If I try print card immidietly after assignment it's ok but if I try it after all assignments nothing happend.
My "code":
int main(void)
{
char rank[] = "23456789TJQKA";
char color[] = "cdhs";
char deck[52][3];
int k = 0;
for (int i = 0; i < 13; i++) {
for(int j = 0; j < 4; j++) {
deck[k][0] = rank[i];
deck[k][1] = color[j];
deck[k][2] = 0;
k++;
printf("%s\n",deck[k-1]); // this print works
}
}
printf("%s\n",deck[0]); //this does nothing (even if I change index)
//-------------------------- here I am trying make all possible pairs but deck is now empty :(
k = 0;
char allPairs[1327][5];
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 52; j++) { //**edit** - thanks ;)
allPairs[k][0] = deck[i][0];
allPairs[k][1] = deck[i][1];
allPairs[k][2] = deck[j][0];
allPairs[k][3] = deck[j][1];
allPairs[k][4] = 0;
k++;
}
}
}
All seems to work now thanks guys!
What you need to do is replace i++ with j++ in the following statement
for (int j = 0; j < 3; i++)
and also comment out the following line as it is printing 2c again:
printf("%s\n",deck[0]); //this does nothing (even if I change index)
I have to make a program that sort strings (with exact length 7 chars) by using radix sort. I already made a function that sort each column separately. My problem is how to make the whole string move, not just one char. It's really problematic for me to see how should it work in C.
I made one array "char strings[3][8]" and "char output[3][8]" to get sorted 3 strings with exact 7 chars in each one. For example sorting these strings:
strcpy(strings[0], "kupbars");
strcpy(strings[1], "daparba");
strcpy(strings[2], "jykaxaw");
In output I get:
dakaaaa
juparbs
kypbxrw
Each column is sorted correctly but chars don't stick together. I tried many ways for 3 hours but nothing works.
My code looks like this:
void countingSort(char a[][8], char b[][8]) {
int c[123];
for (int pos = 6; pos >= 0; pos--) {
for (int i = 0; i < 123; i++)
c[i] = 0;
for (int i = 0; i < 3; i++)
c[(int)a[i][pos]]++;
for (int i = 1; i < 123; i++)
c[i] += c[i - 1];
for (int i = 2; i >= 0; i--) {
b[--c[(int)a[i][pos]]][pos] = a[i][pos];
}
}
}
(There are constants limiting string length etc. because it's easy to change it to variable - I just focused on getting this program work properly.)
Try changing the loop to move an entire string:
for (int i = 2; i >= 0; i--) {
int k = --c[(int)a[i][pos]];
for(int j = 0; j < 8; j++) {
b[k][j] = a[i][j];
}
}
You could do a circular list but it's a little overhead. I propose you to use memmove().
#include <string.h>
void array_move_forward(char array[3][8]) {
for (int i = 0; i < 3; i++) {
char tmp = array[i][6];
memmove(array[i] + 1, array[i], 6);
array[i][0] = tmp;
}
}
void array_move_rewind(char array[3][8]) {
for (int i = 0; i < 3; i++) {
char tmp = array[i][0];
memmove(array[i], array[i] + 1, 6);
array[i][6] = tmp;
}
}
A other solution would be to manipulate your string yourself and using a index, that indicate the first letter of your string.
{
char str[7];
int i = 0;
...
int j = i;
for (int k = 0; k < 7; k++) {
char tmp = str[j++ % 7];
}
}
With that you could rotate your string just with i++ or i--.
struct my_string_radix {
char str[7];
int begin;
}
I have a string where I need to do substring operation. I'm trying to achieve something like this for example if the input string is com then the output must be something like this -
c
co
com
o
om
m.. I have tried this
for(int i=0 ; i<len ;i++)
{
printf("%s",&string[strlen(string)-i]));
}
A substring is defined by its left and right ends so there are O(n*n) substrings in a string of length n.
int n = strlen(string);
for(int i = 0; i < n; i++)
{ for(int j = i; j < n; j++)
{ /* print substring from i to j */
for(int k = i; k <= j; k++)
{ printf("%c", string[k]);
}
printf("\n");
}
}
You're missing a comma in your code:
for(int i=0 ; i<len ;i++)
{
printf("%s", &string[strlen(string)-i])
}
But that will print "", "m", "om" - not what you want.
Something more like:
// start at each point in the string
for ( const char *start = string; *start; ++start )
{
// for each starting point, go from the whole remainder down
// to just one character
for ( const char *end = string + strlen(string); end > start; --end )
{
for ( const char *s = start; s < end; ++s )
putchar(*s);
putchar('\n');
}
}
Example: https://ideone.com/XXoYv6
Substring means any contiguous group of characters.
For the n string
it will generate (n*(n-1) /2) substrings.
For example of String
source = "STACK"
Length of the character is 5, so it total substring would be (5(5-1) /2) = 10
We have to iterate through the first element of string and print all the substring and than onwards one by one we increment the index of i and printing the substring from range (j to k)
public void generateSubString(String source){
char[] arr = source.toCharArray();
for(int i = 0; i<arr.length; i++){
for(int j = i ; j < arr.length; j++){
for(int k = i; k<=j; k++){
System.out.print(arr[k]);
}
System.out.println();
}
OUTPUT:
S
ST
STA
STAC
STACK
T
TA
TAC
TACK
A
AC
ACK
C
CK
K
Below is the code to find all substrings of a string in javascript without for loop, which will increase the speed of code.
const devideSubStr = (str) => {
var totalLoop = str.length * ((str.length + 1)/2);
// looping count
let i = 0;
var totalChar = 1;//character to get
var charFrom = 0;// from which index
var strLength = str.length;//length of digit
while( i < totalLoop){
console.log(str.substr(charFrom, totalChar))
charFrom ++;
i ++;
if(charFrom == strLength){
charFrom = 0;
strLength = strLength - 1;
totalChar ++;
}
}}
I have a 2d array which contains for example
dict[0][30] = "name1";
dict[1][30] = "name2";
dict[2][30] = "name3";
i'm using the following function to check for occurrences
char letters[] = {"abcdefghijklmnopqrstuvwxyz"};
for(i = 0; i < size; i++)
for (j = 0; j < 26; j++)
if(tolower(dict[i]) == letters[j])
count[j]++;
i have tested the code using 1d array and its working any example for 2d arrays
Thanks
dict[0][30] = "name1";
check if it is defining the variable.